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

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>
相关推荐
神仙别闹3 分钟前
基于Java+MySQL实现(Web)可扩展的程序在线评测系统
java·前端·mysql
心.c18 分钟前
react当中的this指向
前端·javascript·react.js
Java水解25 分钟前
Web API基础
前端
闲鱼不闲26 分钟前
实现iframe重定向通知父级页面跳转
前端
咸鱼青菜好好味27 分钟前
node的项目实战相关-2-前台接口
前端
春秋半夏28 分钟前
音乐播放、歌词滚动
前端·css
Sioncovy32 分钟前
Zustand 源码阅读计划(3)- JS 篇 - Middlewares 中间件逻辑
前端·javascript
bo5210033 分钟前
垃圾回收机制详解
前端
多啦C梦a35 分钟前
🪄 这么优雅?`useContext` + 自定义 Hooks:优雅管理全局状态,从主题切换说起
前端·javascript·react.js
患得患失9491 小时前
【前端】【Echarts】ECharts 词云图(WordCloud)教学详解
前端·javascript·echarts