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

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>
相关推荐
DN金猿9 分钟前
使用npm install或cnpm install报错解决
前端·npm·node.js
丘山子10 分钟前
一些鲜为人知的 IP 地址怪异写法
前端·后端·tcp/ip
志存高远6622 分钟前
Kotlin 的 suspend 关键字
前端
www_pp_34 分钟前
# 构建词汇表:自然语言处理中的关键步骤
前端·javascript·自然语言处理·easyui
天天扭码1 小时前
总所周知,JavaScript中有很多函数定义方式,如何“因地制宜”?(ˉ﹃ˉ)
前端·javascript·面试
一个专注写代码的程序媛1 小时前
为什么vue的key值,不用index?
前端·javascript·vue.js
장숙혜1 小时前
ElementUi的Dropdown下拉菜单的详细介绍及使用
前端·javascript·vue.js
火柴盒zhang1 小时前
websheet之 编辑器
开发语言·前端·javascript·编辑器·spreadsheet·websheet
某公司摸鱼前端2 小时前
uniapp 仿企微左边公司切换页
前端·uni-app·企业微信
WKK_2 小时前
uniapp自定义封装tabbar
前端·javascript·小程序·uni-app