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

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>
相关推荐
小小小小宇15 分钟前
请求竞态问题统一封装
前端
loriloy16 分钟前
前端资源帖
前端
源码超级联盟17 分钟前
display的block和inline-block有什么区别
前端
GISer_Jing23 分钟前
前端构建工具(Webpack\Vite\esbuild\Rspack)拆包能力深度解析
前端·webpack·node.js
让梦想疯狂25 分钟前
开源、免费、美观的 Vue 后台管理系统模板
前端·javascript·vue.js
海云前端1 小时前
前端写简历有个很大的误区,就是夸张自己做过的东西。
前端
葡萄糖o_o1 小时前
ResizeObserver的错误
前端·javascript·html
AntBlack1 小时前
Python : AI 太牛了 ,撸了两个 Markdown 阅读器 ,谈谈使用感受
前端·人工智能·后端
MK-mm1 小时前
CSS盒子 flex弹性布局
前端·css·html
小小小小宇2 小时前
CSP的使用
前端