分别用普通过程,面向对象过程,和继承方法实现拖拽效果
2014-08-22 02:13
696 查看
首先我们在这里先写一个正常逻辑下,一个面向过程的js拖拽demo
接下来我们如何去转为面向对象编码模式
在这里其实很简单,只要将变量抽象出来作为属性,放在构造函数里面,在将原本的函数作为原型方法就可以了
跟着我们再来看看,继承的拖拽吧
在这里,我们通过继承的方式实现了LimitDrag对象,仅仅简单的对其做了左右的限制,还为完善····
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title></title> <style type="text/css"> div{width: 200px;height:200px;background:red;position: absolute;} </style> </head> <body> <div></div> <script type="text/javascript"> window.onload=function(){ var oDiv=document.getElementsByTagName('div')[0]; oDiv.onmousedown=function(e){ var ev=e||event; var posX=ev.clientX-oDiv.offsetLeft; var posY=ev.clientY-oDiv.offsetTop; document.onmousemove=function(ev){ var oEvent=ev||event; oDiv.style.left=oEvent.clientX-posX+'px'; oDiv.style.top=oEvent.clientY-posY+'px'; }; document.onmouseup=function(){ document.onmousemove=null; document.onmouseup=null; } }; } </script> </body> </html>
接下来我们如何去转为面向对象编码模式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title></title> <style type="text/css"> div{width: 200px;height:200px;background:red;position: absolute;} </style> </head> <body> <div id="div1"></div> <script type="text/javascript"> window.onload=function(){ new Drag('div1'); } function Drag(id){ this.posX=0; this.posY=0; var _this=this; this.oDiv=document.getElementById(id); this.oDiv.onmousedown=function(ev){ _this.fnDw(ev); } } Drag.prototype.fnDw=function(ev){ var _this=this; var oEvent=ev||event; this.posX=oEvent.clientX-this.oDiv.offsetLeft; this.posY=oEvent.clientY-this.oDiv.offsetTop; document.onmousemove=function(ev){ _this.fnMove(ev);} document.onmouseup=function(){ _this.fnUp(); } } Drag.prototype.fnMove=function(ev){ var oEvent=ev||event; this.oDiv.style.left=oEvent.clientX-this.posX+'px'; this.oDiv.style.top=oEvent.clientY-this.posY+'px'; } Drag.prototype.fnUp=function(){ document.onmousemove=null; document.onmouseup=null; } </script> </body> </html>
在这里其实很简单,只要将变量抽象出来作为属性,放在构造函数里面,在将原本的函数作为原型方法就可以了
跟着我们再来看看,继承的拖拽吧
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title></title> <style type="text/css"> #div1{width: 200px;height:200px;background:red;position: absolute;} #div2{width: 200px;height:200px;background:blue;position: absolute;} </style> </head> <body> <div id="div1"></div> <div id="div2"></div> <script type="text/javascript"> window.onload=function(){ new Drag('div1'); new LimitDrag('div2'); } function Drag(id){ this.posX=0; this.posY=0; var _this=this; this.oDiv=document.getElementById(id); this.oDiv.onmousedown=function(ev){ _this.fnDw(ev); } } Drag.prototype.fnDw=function(ev){ var _this=this; var oEvent=ev||event; this.posX=oEvent.clientX-this.oDiv.offsetLeft; this.posY=oEvent.clientY-this.oDiv.offsetTop; document.onmousemove=function(ev){ _this.fnMove(ev);} document.onmouseup=function(){ _this.fnUp(); } } Drag.prototype.fnMove=function(ev){ var oEvent=ev||event; this.oDiv.style.left=oEvent.clientX-this.posX+'px'; this.oDiv.style.top=oEvent.clientY-this.posY+'px'; } Drag.prototype.fnUp=function(){ document.onmousemove=null; document.onmouseup=null; } //继承 function LimitDrag(id){ Drag.call(this,id); } for(var i in Drag.prototype){ LimitDrag.prototype[i]=Drag.prototype[i]; } LimitDrag.prototype.fnMove=function(ev){ var oEvent=ev||event; var l=oEvent.clientX-this.posX; var t=oEvent.clientY-this.posY; if(l<0){ l=0; }else if(l>document.documentElement.clientWidth-this.oDiv.offsetWidth){ l=document.documentElement.clientWidth-this.oDiv.offsetWidth; } this.oDiv.style.left=l+"px"; this.oDiv.style.top=t+"px"; } </script> </body> </html>
在这里,我们通过继承的方式实现了LimitDrag对象,仅仅简单的对其做了左右的限制,还为完善····
相关文章推荐
- 面向对象,类的组合关系,继承,实现,方法重写,方法重载,this的使用,抽象方法和抽象类的比较,父类构造方法存在的意义,多态的是用和解析,各种访问修饰符
- javascript面向对象实现方法(类的继承与多态)
- 面向对象银角大王补充2-self就是调用当前方法的对象-静态字段,公有属性-封装的理解-继承的理解,普通方法,静态方法
- 面向对象---基于组合的继承:属性的继承基于伪装的方式实现,而方法的继承基于原型链的方式继承。
- Javascript实现最简跨平台面向对象拖拽
- essential c++ 基于第二章的内容采用面向过程的方法实现猜字游戏的基本实现代码
- jquery层拖拽效果的实现方法
- JAVA面向对象基础:继承、变量隐藏、方法重写、包、修饰符、this、super
- C#高级——创建Person类,分别使用面向过程的方法和面向对象的方法
- Javascript实现最简跨平台面向对象拖拽
- 分别使用面向过程的方法和面向对象的方法,完成如下命题:求 两点之间的距离。
- 使用MVC中的HtmlHelper对象的ActionLink方法,实现一个Javascript:void(0)效果,去调用一个JS方法
- 从普通函数到对象方法 ------Windows窗口过程的面向对象封装
- javascript 面向对象,实现namespace,class,继承,重载
- Js实现拖拽 --面向对象封装( 超详细中文注释)
- 从普通函数到对象方法 ------Windows窗口过程的面向对象封装
- Javascript实现最简跨平台面向对象拖拽
- 从普通函数到对象方法 ------Windows窗口过程的面向对象封装
- 面向对象高级(继承的基本实现、进一步研究、final关键字)
- JAVA中创建线程对象的两种方法:继承Thread和实现Runable