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>
相关推荐
爱上妖精的尾巴4 小时前
8-18 WPS JS宏 正则表达式-边界匹配
开发语言·javascript·正则表达式·wps·jsa
爱上妖精的尾巴4 小时前
8-20 WPS JS宏 正则表达式-懒惰匹配
服务器·前端·javascript
网络点点滴4 小时前
组件通信props方式
前端·javascript·vue.js
weixin_443478514 小时前
flutter组件学习之Flex / Expanded弹性布局组件
javascript·学习·flutter
SuperEugene4 小时前
Excel 上传解析 + 导出实战:Vue+xlsx 避坑指南|Vue生态精选
前端·javascript·vue.js·excel·xlsx·vxetable
Highcharts.js4 小时前
使用Highcharts创建流图(Stream Graph)指南|流动数据的可视化图表与数据艺术表达
javascript·信息可视化·数据可视化·highcharts·可视化图表·流图·stream graph
极客小俊4 小时前
【H5 前端开发笔记】第 03 期:HTML的历史、书写规范与文档类型声明<!DOCTYPE> 详解
html·学习笔记·前端开发·编程基础·免费教程·html文档类型声明·零基础学习
努力往上爬de蜗牛4 小时前
el-table列表修改某个输入框输入 卡顿问题修改
javascript·vue.js·elementui
湛海不过深蓝4 小时前
【procomponents】根据表单查询表格数据的两种写法
前端·javascript·react.js