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

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>
相关推荐
ZC跨境爬虫2 分钟前
海南大学交友平台开发实战day7(实现核心匹配算法+解决JSON请求报错问题)
前端·python·算法·html·json
下北沢美食家5 分钟前
CSS面试题2
前端·css
weixin_4617694012 分钟前
npm create vue@latest 错误
前端·vue.js·npm
WindrunnerMax13 分钟前
从零实现富文本编辑器#13-React非编辑节点的内容渲染
前端·架构·github
四千岁13 分钟前
Ollama+OpenWebUI 最佳组合:本地大模型可视化交互方案
前端·javascript·后端
写不来代码的草莓熊16 分钟前
el-date-picker ,自定义输入数字自动转换显示yyyy-mm-dd HH:mm:ss格式
前端·javascript·vue.js
ssshooter16 分钟前
Tauri 应用苹果签名踩坑实录
前端·架构·全栈
DeSheng20 分钟前
npm 从入门到精通(二):再理解,彻底搞懂 package.json、node_modules 和 package-lock
前端
用户693717500138420 分钟前
XChat 为什么选择 Rust 语言开发
android·前端·ios
局i21 分钟前
从零搭建 Vite + React 项目:从环境准备到干净项目的完整指南
前端·react.js·前端框架