码迷,mamicode.com
首页 > 其他好文 > 详细

跟随鼠标指针跑的div拖拽效果

时间:2018-04-17 19:36:02      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:height   client   div2   position   div   sem   htm   XML   var   

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
       #div1 {
           height:100px;
           width:100px;
           background-color:#0094ff;
           position:absolute;
        }
    </style>
</head>
<body>
    <div id="div1"></div>
    <div id="div2"></div>
    <script>
       var  div1 = document.getElementById("div1");
       var  div2 = document.getElementById("div2");
       div1.onmousedown=function(e) {
            var er=e||event;
            var disx=er.offsetX;
            var disy = er.offsetY;
           div1.onmousemove=function(e1) {
                var er1 = e1 || event;
                var x = er1.clientX - disx;
                var y = er1.clientY - disy;
           
            div1.style.left =  x + "px";
            div1.style.top =  y + "px";
           }
       }
       div1.onmouseup = function () {
           div1.onmousemove = null;
       }
    </script>
</body>
</html>

 

跟随鼠标指针跑的div拖拽效果

标签:height   client   div2   position   div   sem   htm   XML   var   

原文地址:https://www.cnblogs.com/lhl66/p/8868373.html

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