让div跟着鼠标移动

朋友求助帖

 

具体实现代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #rianley{
            width: 50px;
            height: 50px;
           background: red;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
    <script>
        document.onmousemove=function(e){
            var rianley=document.getElementById("rianley");
            rianley.style.left=e.clientX+"px";
            rianley.style.top=e.clientY+"px";
        }
    </script>
</head>
<body>
    <div id="rianley"></div>
</body>
</html>

  

说明 

转载请注明出处

 

posted @ 2018-08-02 17:04  rianley  阅读(6545)  评论(0编辑  收藏  举报