码迷,mamicode.com
首页 > 移动开发 > 详细

样式根据鼠标的移动而移动

时间:2018-11-03 14:39:06      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:www   https   var   tar   doc   height   ||   code   top   

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js 鼠标移动</title>
    <style>
        #div1{
            width: 50px;
            height: 50px;
            background: red;
            position: absolute;   /*这个是关键*/
        }
    </style>
    <script>
        //效果是样式跟着鼠标的移动而移动
        document.onmousemove=function(ev) {  //鼠标移动
            var oEvent=ev||event;
            var oDiv=document.getElementById("div1");

            oDiv.style.left=oEvent.clientX+"px";
            oDiv.style.top=oEvent.clientY+"px";

        }
    </script>
</head>
<body>
    <div id="div1"></div>
</body>
</html>

 如果高度,宽度很大,解决方法:https://www.cnblogs.com/quitpoison/p/9900714.html

样式根据鼠标的移动而移动

标签:www   https   var   tar   doc   height   ||   code   top   

原文地址:https://www.cnblogs.com/quitpoison/p/9900679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!