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>
相关推荐
wuhen_n13 分钟前
副作用的概念与effect基础:Vue3响应式系统的核心
前端·javascript·vue.js
张3蜂14 分钟前
Vue.js-知识体系
前端·javascript·vue.js
wuhen_n22 分钟前
effect函数的完整实现与追踪:深入Vue3响应式核心
前端·javascript·vue.js
Never_Satisfied26 分钟前
在JavaScript / HTML中,img标签loading lazy加载时机详解
开发语言·javascript·html
小飞大王66628 分钟前
WebSocket技术与心跳检测
前端·javascript·websocket·网络协议·arcgis
a11177631 分钟前
波浪圆圈背景效果(html 开源)
前端·html
滕青山42 分钟前
HTML编码/解码 核心JS实现
前端·javascript·vue.js
RunsenLIu1 小时前
智慧房屋租赁管理系统
前端·javascript·vue.js