html+css+js实现小红点跟随鼠标移动

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;padding: 0;
        }
        .box{
            width: 100vw;
            height: 100vh;
            /* 该元素不能对鼠标事件做出反应 */
            /* pointer-events: none; */
            overflow: hidden;
            background: pink;
        }
        .dot {
            width: 20px;
            height: 20px;
            background-color: red;
            border-radius: 50%;
            position: absolute;
            top: 0px;
            left: 0px;
            transform: translate(-100%,-100%);
            /* 小球跟随鼠标的速度 越小越快 */
            transition: all 0.1s;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="dot"></div>
    </div>
    <script>
        const rdot = document.querySelector('.dot');
        // 监听事件:鼠标被移动
        document.addEventListener('mousemove',function(e){
            rdot.style.top = e.pageY + 'px';
            rdot.style.left = e.pageX + 'px';
        })
        // 鼠标指针移动到元素上时触发
        document.addEventListener('mouseenter',function(){
            rdot.style.opacity = 1
        })
        // 鼠标指针移出元素时触发
        document.addEventListener('mouseleave',function(){
            rdot.style.opacity = 0
        })
    </script>
</body>

</html>
相关推荐
万少25 分钟前
产品原型不用从零画 -GPT 出图,Gemini 生成 HTML
前端·javascript·后端
ZC跨境爬虫2 小时前
跟着 MDN 学JavaScript day_9:字符串方法实战挑战与解题思路
开发语言·前端·javascript
hewins2 小时前
NestJS 从入门到精通
javascript
柒和远方2 小时前
LeetCode 452. 用最少数量的箭引爆气球 —— 区间贪心经典:排序 + 扫描一箭穿心
javascript·python·算法
小小龙学IT3 小时前
Drizzle ORM:TypeScript 生态中冉冉升起的数据库工具链引言
javascript·数据库·typescript
旺王雪饼 www4 小时前
localStorage 和 sessionStorage区别与联系
服务器·前端·javascript
এ慕ོ冬℘゜4 小时前
【双月日期范围选择器】博客(可直接交作业 / 上线)
前端·javascript·交互·jquery
VidDown4 小时前
VidDown 使用介绍:一个免费、本地化的在线工具集
javascript·编辑器·音视频·视频编解码·视频
小牛itbull5 小时前
告别传统主题开发!ReactPress Theme Starter —— 用 Next.js 15 构建现代化无头博客
javascript·cms·react·wordpress·nextjs·reactpress·blog-theme
深蓝电商API5 小时前
逆向工程入门:从Chrome DevTools到JS混淆还原
前端·javascript·chrome·爬虫·chrome devtools