js实现鼠标跟随

<style>
        body {
            background-color: black;
            /* 吧鼠标隐藏 */
            cursor: none;
        }
        
        #box {
            width: 100px;
            height: 100px;
            /*  background-image: url(./img/src=http\ _hbimg.b0.upaiyun.com_f3036a3b5396956d8a3854d4f245f2597795fc8a4b269-YIXvKe_fw658&refer=http\ _hbimg.b0.upaiyun.com&app=2002&size=f9999\,10000&q=a80&n=0&g=0n&fmt=jpeg.gif);
            background-repeat: no-repeat;
            background-position: center;
            background-size: 150px; */
            background-color: rgb(156, 156, 156);
            position: absolute;
        }
    </style>
</head>

<body>
    <div id="box">

    </div>
    <script>
        /* 获取box */
        var box = document.getElementById('box')
            /* 绑定鼠标移动事件 */
        document.onmousemove = function(e) {
            /* 获取x位置 */
            var x = e.pageX;
            /* 获取y位置 */
            var y = e.pageY;
            /* 设置左右,通过定位 */
            box.style.left = x - (box.offsetWidth / 2) + 'px'
            box.style.top = y - (box.offsetHeight / 2) + 'px'
        }
    </script>
</body>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值