当前位置:网站首页>Js- get the mouse coordinates and follow them

Js- get the mouse coordinates and follow them

2022-06-25 14:54:00 -multiflora-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        #box1{
    
            width: 100px;
            height: 100px;
            background-color: #bbffaa;
            /*  Turn on box1 The absolute positioning of  */
            position: absolute;
        }
    </style>
    <script type="text/javascript">
        window.onload = function (){
    
            /* *  bring div You can follow the mouse  * */
            var box1 = document.getElementById("box1");
            document.onmousemove = function (event){
    
                // Solve compatibility problems 
                event = event || window.event

                // Get mouse coordinates 
                var left = event.pageX;
                var top = event.pageY;

                // Set up div The offset 
                box1.style.left = left + "px";
                box1.style.top = top + "px";
            }
        }
    </script>
</head>
<body>
    <div id="box1"></div>
</body>
</html>

 Insert picture description here

原网站

版权声明
本文为[-multiflora-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200517234548.html