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>
相关推荐
qq_12084093718 分钟前
Three.js 模型加载与线上稳定性实战:路径、跨域、缓存与降级全链路指南
开发语言·javascript·缓存·vue3
qq_120840937116 分钟前
Vue3 + Three.js 实战入门:从零搭建可交互3D场景(含模型加载与性能优化)
javascript·3d·vue3·交互
1314lay_100717 分钟前
axios的Post方法和Delete方法的参数个数和位置不同,导致415错误
前端·javascript·vue.js·elementui
LXXgalaxy24 分钟前
HTML头部元信息避坑指南
前端·html
qq_3813385025 分钟前
现代 CSS 黑科技:Container Queries、:has() 与嵌套语法实战
前端·css·科技
不恋水的雨33 分钟前
html中补齐table表格合并导致每行td数量不一致的情况
前端·html
qq_120840937135 分钟前
Vue3 + Three.js 入门实战:从 0 到 1 搭建可交互的 3D 场景(含模型加载与性能优化)
javascript·3d·vue3·交互·webgl·gltf
iReachers35 分钟前
HTML打包EXE工具四种弹窗方式图文详解 - 单窗口/新窗口/标签页/浏览器打开
前端·javascript·html·弹窗·html打包exe·html转程序
耗子君QAQ1 小时前
🔧 Rattail | 面向 Vite+ 和 AI Agent 的前端工具链
前端·javascript·vue.js
萑澈9 小时前
Windows 7 运行 Electron 安装包报“不是有效的 Win32 应用程序”怎么办
javascript·windows·electron