使用Vue实现点击页面触发特效

 效果描述 

在页面上的指定区域内进行点击,则会在页面上显示设置好的随机文本,此文本出现后会执行动画,动画效果为节点在1s之内向右上方移动并在移动的过程中完成180°翻转,最后消失。

效果展示

完整代码

<template>
  <div ref="container" class="box" @click="handleRandom">
    <span
      :key="childKey"
      ref="child"
      :style="{ color: colorRandom(), left: childLeft, top: childTop }"
      class="minbox"
    >
      {{ msg }}
    </span>
  </div>
</template>

<script setup>
import { ref } from "vue";
const container = ref(null);
const child = ref(null); // 鼠标点击后,在页面展示文本的节点
const childKey = ref(0); // 每次点击后,改变key以触发Vue重新渲染span
// 生成随机颜色
const colorRandom = () => {
  let color;
  do {
    const r = Math.floor(Math.random() * 256);
    const g = Math.floor(Math.random() * 256);
    const b = Math.floor(Math.random() * 256);
    color = `rgb(${r},${g},${b})`;
  } while (color === "rgb(250,235,215)"); // 检查颜色是否是我们不想要的
  return color;
};
const textArr = [
  "( ^∀^)",
  "富强",
  "Σ(゚д゚;)",
  "民主",
  "(⊙o⊙)",
  "文明",
  "✈",
  "和谐",
  "☯",
  "自由",
  "☠",
  "平等",
  "〠",
  "公正",
  "❤",
  "法治",
  "爱国",
  "★",
  "敬业",
  "诚信",
  "友善",
];
// 生成随机数
const randomInd = () => {
  return Math.floor(Math.random() * textArr.length);
};
const msg = ref(null); //在页面展示的文本
const childLeft = ref(0);
const childTop = ref(0);
// 鼠标点击事件
const handleRandom = (e) => {
  childLeft.value = e.clientX + "px";
  childTop.value = e.clientY + "px";
  msg.value = textArr[randomInd()];
  // 在每次点击后,改变key以触发Vue重新渲染span
  childKey.value++;
};
</script>

<style lang="scss" scoped>
.box {
  width: 500px;
  height: 300px;
  cursor: pointer;
  background-color: rgb(250, 235, 215);
  .minbox {
    width: fit-content;
    position: absolute;
    animation: moveAndFadeout 1s ease-in-out forwards;
  }
  // 移动和、淡出以及翻转动画
  @keyframes moveAndFadeout {
    0% {
      transform: translate(0, 0) scale(1) rotate(0deg);
      opacit: 1;
    }
    100% {
      transform: translate(20px, -80px) rotate(180deg) scale(2);
      opacity: 0;
    }
  }
}
</style>

相关推荐

  1. uniapp 实现赞出现特效

    2024-07-19 11:40:03       44 阅读
  2. 解决vue按钮刷新页面的一个bug

    2024-07-19 11:40:03       47 阅读
  3. vue触发真实的 事件 跟用户行为一致

    2024-07-19 11:40:03       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 11:40:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 11:40:03       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 11:40:03       58 阅读
  4. Python语言-面向对象

    2024-07-19 11:40:03       69 阅读

热门阅读

  1. 最长上升子序列模板(LIS)

    2024-07-19 11:40:03       21 阅读
  2. Apache-BeanUtils VS SpringBean-Utils

    2024-07-19 11:40:03       15 阅读
  3. MySQL中为什么不推荐使用 text 类型?

    2024-07-19 11:40:03       18 阅读
  4. 华为云认证

    2024-07-19 11:40:03       19 阅读
  5. TF和TF-IDF区别和联系

    2024-07-19 11:40:03       18 阅读
  6. K8S内存资源配置

    2024-07-19 11:40:03       22 阅读