前端小技巧-网页点击动画效果

bash 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>点击触发动画效果</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <style>
    /* styles.css */
    @keyframes riseAndFade {
      from {
        transform: translateY(0);
        opacity: 1;
      }
      to {
        transform: translateY(-100px);
        opacity: 0;
      }
    }

    body {
      margin: 0;
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #f0f0f0;
      cursor: pointer;
    }

    .rise-fade {
      position: absolute;
      font-size: 20px;
      font-weight: 700;
      white-space: nowrap;
      animation: riseAndFade 1.5s ease forwards;
      pointer-events: none;
    }
  </style>
  <body>
    <script>
      // script.js
      document.addEventListener("DOMContentLoaded", function () {
        var phrases = [
          "VUE2!",
          "VUE3!",
          "React",
          "Node",
          "Element UI",
        ];

        document.body.addEventListener("click", function (event) {
          var randomPhrase =
            phrases[Math.floor(Math.random() * 6)];
          var textElement = document.createElement("div");
          textElement.className = "rise-fade";
          textElement.textContent = randomPhrase;
          textElement.style.color = "hsl(" + Math.random() * 360 + ", 50%, 50%)";

          document.body.appendChild(textElement);
          textElement.style.left =
            event.pageX - textElement.offsetWidth / 2 + "px";
          textElement.style.top = event.pageY - textElement.offsetHeight + "px";

          setTimeout(function () {
            textElement.remove();
          }, 1100); // 100ms延迟确保动画完成
        });
      });
    </script>
  </body>
</html>
相关推荐
奔跑的web.2 小时前
TypeScript Enum 类型入门:从基础到实战
前端·javascript·typescript
盐真卿3 小时前
python2
java·前端·javascript
梦梦代码精3 小时前
BuildingAI vs Dify vs 扣子:三大开源智能体平台架构风格对比
开发语言·前端·数据库·后端·架构·开源·推荐算法
seabirdssss4 小时前
《bootstrap is not defined 导致“获取配置详情失败”?一次前端踩坑实录》
前端·bootstrap·html
kgduu4 小时前
js之表单
开发语言·前端·javascript
谢尔登6 小时前
Vue3 响应式系统——computed 和 watch
前端·架构
愚公移码6 小时前
蓝凌EKP产品:主文档权限机制浅析
java·前端·数据库·蓝凌
欣然~7 小时前
法律案例 PDF 批量转 TXT 工具代码
linux·前端·python
一个小废渣7 小时前
Flutter Web端网络请求跨域错误解决方法
前端·flutter
符文师8 小时前
css3 新特性
前端·css3