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>
相关推荐
Lao乾妈官方认证唯一女友:D5 小时前
通过plasmo的wallet扩展添加新钱包
javascript·web3·区块链
Javatutouhouduan5 小时前
SpringBoot整合reids:JSON序列化文件夹操作实录
java·数据库·redis·html·springboot·java编程·java程序员
ALKAOUA5 小时前
力扣面试150题刷题分享
javascript·笔记
怪侠_岭南一只猿5 小时前
爬虫工程师入门阶段一:基础知识点完全学习文档
css·爬虫·python·学习·html
swipe5 小时前
JavaScript 对象与属性描述符:从原理到实战
前端·javascript·面试
&活在当下&5 小时前
Vue3 h函数用法详解
前端·javascript·vue.js
小贵子的博客5 小时前
(vue3错误处理)has naming conflicts with other components, ignored.
前端·javascript·vue.js
西西学代码6 小时前
Flutter---路由与导航
服务器·前端·javascript
认真的小羽❅7 小时前
HTML完全指南:从入门到精通
html