JS小案例甩不掉的跟屁虫

根本鼠标的XY值,让元素移动

<body>
    <img src="image/toux.ico" id="imgbug" alt="移动图片">
</body>
<script type="text/javascript">
    window.onload = function () {
        var ima = document.getElementById("imgbug");
        ima.style.position = "absolute";//让图片绝对定位
        //注册鼠标的移动处理函数
        document.onmousemove = function () {
            //改变图片的坐标
            //获取鼠标的坐标
            var x = event.clientX;
            var y = event.clientY;
            ima.style.left = x-ima.offsetWidth+"px";
            ima.style.top = y - 76+"px";
        }
    }
</script>









猜你喜欢

转载自blog.csdn.net/asdtp/article/details/79876422