资讯专栏INFORMATION COLUMN

前端小白用面向对象思想实现元素拖拽

springDevBird / 960人阅读

摘要:上篇文章分享了如何用面向对象思想编写选项卡,在文章最后留了一个拖拽的例子,希望大家可以试着写一下,现在我就谈谈我在这过程中遇到的一些问题和解决方法。通过以上方法来训练面向对象的编程思想,多练习,以后写出面向对象思想的代码就很简单了。

上篇文章分享了如何用面向对象思想编写选项卡,在文章最后留了一个拖拽的例子,希望大家可以试着写一下,现在我就谈谈我在这过程中遇到的一些问题和解决方法。(本文主要是想和js初学者分享经验,代码中的更改this指向,事件绑定均采用的比较初级的方法,也希望能有大神能指导,分享一下经验)。

现在我们来看看面向过程式的编程的代码,HTML只有一个div元素,设置宽高和背景颜色。

#div1{
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
        }

    
window.onload=function(){ var oDiv=document.getElementById("div1"); var disX=0; var disY=0; oDiv.onmousedown=function(ev){ var ev=ev || window.event; disX=ev.clientX-oDiv.offsetLeft; disY=ev.clientY-oDiv.offsetTop; document.onmousemove=function(ev){ var ev=ev || window.event; oDiv.style.left=ev.clientX-disX+"px"; oDiv.style.top=ev.clientY-disY+"px"; }; document.onmouseup=function(){ document.onmousemove=null; document.onmouseup=null; } return false; } }

还是用上一篇文章提到的方法,先将变量和方法提取出来,不要出现函数的嵌套,出现嵌套的将其中的函数提取出来。变量为oDiv,disX和disY,我将代码中的三个事件绑定的函数命名为fnDown,fnMove,fnUp,因此将上面的代码改为如下格式:

var oDiv,disX,disY;
        window.onload=function(){
            oDiv=document.getElementById("div1");
            
            disX=0;
            disY=0;
            oDiv.onmousedown=fnDown();
        }
        
        
        function fnDown(ev){
            var ev=ev || window.event;
            disX=ev.clientX-oDiv.offsetLeft;
            disY=ev.clientY-oDiv.offsetTop;
            document.onmousemove=fnMove();
            document.onmouseup=fnUp();
            return false;
        }
        function fnMove=function(ev){
                var ev=ev || window.event;
                oDiv.style.left=ev.clientX-disX+"px";
                oDiv.style.top=ev.clientY-disY+"px";
            };
        function fnUp=function(){
                document.onmousemove=null;
                document.onmouseup=null;
            }

然后用面向对象的思想将属性和方法添加到对象中,并在属性和方法前面加上this,因此改变的代码如下:

window.onload=function(){
        var d1=new Drag("div1");
        d1.init();
    };
    function Drag(id){
        this.oDiv=document.getElementById(id);
        this.disX=0;
        this.disY=0;
    };
    Drag.prototype.init=function(){
        this.oDiv.onmousedown=this.fnDowm();
    };
    Drag.prototype.fnDowm=function(ev){
        var ev=ev || window.event;
        this.disX=ev.clientX-this.oDiv.offsetLeft;
        this.disY=ev.clientY-this.oDiv.offsetTop;
        document.onmousemove=this.fnMove();
        document.onmouseup=this.fnUp();
        return false;
    };

    Drag.prototype.fnMove=function(ev){
        var ev || window.event;
        this.oDiv.style.left=ev.clientX-this.disX+"px";
        this.oDiv.style.top=ev.clientY-this.disY+"px";
    };

    Drag.prototype.fnMove=function(){
        document.onmousemove=null;
        document.onmouseup=null;
    };

和前面一样,找出其中this的指向错误并改正。但是除了this指向问题,上面还有一个关于事件的问题。document.onmousemove=this.fnMove();,向这样在事件上添加方法的方式是不正确的,正确的调用方式是这样:document.onmousemove=function(ev){};,因此将方法改为以下形式。

Drag.prototype.init=function(){
            
            this.oDiv.onmousedown=function(ev){
                var ev=ev || window.event;
                this.fnDown(ev);
                return false;
            };
        };

        Drag.prototype.fnDown = function(ev){
            
            this.disX = ev.clientX - this.oDiv.offsetLeft;
            this.disY = ev.clientY - this.oDiv.offsetTop;
                
            document.onmousemove = function(ev){
                var ev = ev || window.event;
                this.fnMove(ev);
            };
            document.onmouseup=function(){
                this.fnUp();
            }
        };


        Drag.prototype.fnMove=function(ev){
            
            this.oDiv.style.left=ev.clientX-this.disX+"px";
            this.oDiv.style.top=ev.clientY-this.disY+"px";
        };
        
        Drag.prototype.fnUp=function(){
            document.onmousemove=null;
            document.onmouseup=null;
        };

最后要找到错误的this指向,并改正。关于this指向,我认为最简单的方法就是看函数是怎么调用的,函数名 "." 左边的就是this的指向。下面我们来举个例子:

Drag.prototype.init=function(){
            
            this.oDiv.onmousedown=function(ev){
                var ev=ev || window.event;
                this.fnDown(ev);
                return false;
            };
        };

原型上面的init()方法的调用方式是d1.init(),因此函数内的this指向就是d1,那么this.oDiv指向就是正确的,但是onmouseover()的调用方式是this.oDiv.onmousedown,其内部this指向就是this.oDiv,而在该函数内部,this.fnDown(ev)语句this指向是oDiv,而oDiv是没有方法和属性的,因此这里的this指向就是错误的,需要修正。
下面的几个方法中this也这来来分析,并将其改为正确的指向。修改this的指向还是和上一篇修改的方法一样。因此改完后代码为:

window.onload=function(){

            var d1=new Drag("div1");
            d1.init();

        };

        function Drag(id){
            this.oDiv=document.getElementById(id);
            this.disX=0;
            this.disY=0;
        }
        Drag.prototype.init=function(){
            var This=this;
            this.oDiv.onmousedown=function(ev){
                var ev=ev || window.event;
                This.fnDown(ev);
                return false;
            };
        };

        Drag.prototype.fnDown = function(ev){
            var This = this;
            this.disX = ev.clientX - this.oDiv.offsetLeft;
            this.disY = ev.clientY - this.oDiv.offsetTop;
                
            document.onmousemove = function(ev){
                var ev = ev || window.event;
                This.fnMove(ev);
            };
            document.onmouseup=function(){
                This.fnUp();
            }
        };


        Drag.prototype.fnMove=function(ev){
            
            this.oDiv.style.left=ev.clientX-this.disX+"px";
            this.oDiv.style.top=ev.clientY-this.disY+"px";
        };
        
        Drag.prototype.fnUp=function(){
            document.onmousemove=null;
            document.onmouseup=null;
        };

这样就可以正常运行了。

作为一名小白,就应该多动脑,多动手。在这过程中你会学到很多。通过以上方法来训练面向对象的编程思想,多练习,以后写出面向对象思想的代码就很简单了。

因为以上代码也是我通过看视频学的,所以还有点疑问,希望有人能解答一下。上面只有onmousedown时间是绑定在oDiv上,后面的时间都是绑定在document上,我试着将后面的事件也绑定在oDiv中,程序也能运行,但是快速拖动就会产生停顿的问题,而在document上就没有类似的情况,这是为什么?

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/82399.html

相关文章

  • 前端小白如何面向对象思想写代码

    摘要:根据以上代码的变形,我们现在用面向对象思想来编写代码,创建构造函数,添加属性及方法。首先构造函数中的,由于是用关键字,因此指向,没问题。同时将指向当前点击的以参数形式传入中,这样一个面向对象思想的代码就写出来了。   本人自学前端近半年,js达到熟练的水平,面向对象思想、this指向有一定的了解,但是要用面向对象思想写代码就一脸懵逼了,最近看到某课堂的视频(里面广告嫌疑,就不说是啥了)...

    Guakin_Huang 评论0 收藏0
  • 前端基础进阶(十):面向对象实战之封装拖拽对象

    摘要:前面几篇文章,我跟大家分享了的一些基础知识,这篇文章,将会进入第一个实战环节利用前面几章的所涉及到的知识,封装一个拖拽对象。不封装对象直接实现利用原生封装拖拽对象通过扩展来实现拖拽对象。 showImg(https://segmentfault.com/img/remote/1460000008699587); 前面几篇文章,我跟大家分享了JavaScript的一些基础知识,这篇文章,...

    Eidesen 评论0 收藏0
  • 【连载】前端个人文章整理-从基础到入门

    摘要:个人前端文章整理从最开始萌生写文章的想法,到着手开始写,再到现在已经一年的时间了,由于工作比较忙,更新缓慢,后面还是会继更新,现将已经写好的文章整理一个目录,方便更多的小伙伴去学习。 showImg(https://segmentfault.com/img/remote/1460000017490740?w=1920&h=1080); 个人前端文章整理 从最开始萌生写文章的想法,到着手...

    madthumb 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<