首页 > 其他 > 详细

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

时间:2018-04-17 18:39:57      阅读:215      评论:0      收藏:0      [点我收藏+]
<!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拖拽效果

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!