面向对象实例--拖拽

上一节用面向对象的方法写了选项卡,这次我们来讲讲拖拽

面向过程的拖拽

拖拽主要是对位置的定义,绑定监听器监听鼠标移动的位置。

<!DOCTYPE html>
<html>
<head>
    <title>拖拽</title>
    <style type="text/css">
        #div1{
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
        }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var div = document.getElementById('div1');
            div.onmousedown = function (ev){
                var oEvent = ev||event;
                var disX = oEvent.clientX - div.offsetLeft;
                var disY = oEvent.clientY - div.offsetTop;
                document.onmouseover = function(ev){
                    div.style.left = oEvent.clientX - disX + 'px';
                    div.style.top = oEvent.clientY - disY + 'px'; 
                }
                document.onmouseup = function(){
                    document.onmouseover = null;
                    document.onmouseup = null;
                }
            }
        }
       
    </script>
</head>
<body>
    <div id="div1"></div>
</body>
</html>

面向对象的拖拽

<!DOCTYPE html>
<html>
<head>
    <title>拖拽</title>
    <style type="text/css">
        #div1{
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
        }
    </style>
    <script type="text/javascript">
        window.onload = function() {
            var a = new t();
        }
        function t() {
            var _this = this;
            this.div = document.getElementById('div1');
            this.disX = 0;
            this.disY = 0;
            
            this.div.onmousedown = function() {
                
                _this.mDown();
            }
        }
        t.prototype.mDown = function (ev) {  
               var _this = this;
               var oEvent = ev||event;
               this.disX = oEvent.clientX - this.div.offsetLeft;
               this.disY = oEvent.clientY - this.div.offsetTop;
               document.onmouseover = function() {
                   _this.mOver();
               }
               document.onmouseup = function(){
                   _this.mUp();
               };
        }
        t.prototype.mOver=function  (ev){
            var oEvent = ev||event;
            this.div.style.left = oEvent.clientX - this.disX + 'px';
            this.div.style.top = oEvent.clientY - this.disY + 'px'; 
        }
        t.prototype.mUp = function() {
            document.onmouseover = null;
            document.onmouseup = null;
        }
    </script>
</head>
<body>
    <div id="div1"></div>
</body>
</html>

注意点

  • 因为mouseover和mouseup是嵌套在mousedown里面的,所以需要在mousedown里面再去定义一次this,不然会报错"_this is not defeined"
  • document.onmouseove是针对整个网页的,所以不需要加上this
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • (续jQuery基础(1)) 第5章 DOM节点的复制与替换 (1)DOM拷贝clone() 克隆节点是DOM的常...
    凛0_0阅读 5,192评论 0 8
  • 总结: 鼠标事件 1.click与dbclick事件$ele.click()$ele.click(handler(...
    阿r阿r阅读 5,531评论 2 10
  • 第1章 鼠标事件 1-1 jQuery鼠标事件之click与dbclick事件 用交互操作中,最简单直接的操作就是...
    mo默22阅读 5,046评论 0 6
  • 我曾喜欢过好几个姑娘,我想大家都知道。有的人说四个,有的人猜十个,我一直对这样的猜测抱有很深的疑惑,因为但凡我如果...
    张晓寿阅读 2,924评论 0 0
  • 峻山城飞雪府。 魔影宗主、巫哲城主并没急着离开,而是透过天心楼准备了丰盛宴席,宴请东伯雪鹰! “都是那北河,引诱我...
    im喵小姐阅读 1,924评论 0 0