面向对象的继承与修改(拖拽实例)
2017-05-26 15:58
197 查看
window.onload = function () { new Drag("div1"); new LimiteDrag("div12"); }; /////////////正常的没有限制范围的Drag函数//////////////////// function Drag(id) { this.oDiv = document.getElementById(id); this.disX = 0; this.disY = 0; var _this = this; this.oDiv.onmousedown = function(){ _this.fnDown(); }; }; Drag.prototype.fnDown = function(ev) { var ev = ev || window.event; var _this = this; this.disX = ev.clientX - this.oDiv.offsetLeft; this.disY = ev.clientY - this.oDiv.offsetTop; document.onmousemove = function(){ _this.fnMove(); }; document.onmouseup = function(){ _this.fnUp(); }; }; Drag.prototype.fnMove = function(ev) { var ev = ev || window.event; this.oDiv.style.left = ev.clientX - this.disX + "px"; this.oDiv.style.top = ev.clientY - this.disY + "px"; }; Drag.prototype.fnUp = function() { document.onmousemove = document.onmouseup = null; }; /////////继承Drag对象,并添加限制范围的方法,变成新的LimiteDrag函数/////////// function LimiteDrag(id) { Drag.call(this,id); // 第一步:执行Drag函数,通过Call(this)来改变this指向 }; for(var i in Drag.prototype){ // 第二步:浅拷贝,拷贝Drag上面所有的原型方法 LimiteDrag.prototype[i] = Drag.prototype[i]; }; LimiteDrag.prototype.fnMove = function(ev) { // 第三步:覆盖并改变从Drag上面拷贝过来的fnMove方法 var ev = ev || window.event; var l = ev.clientX - this.disX; var t = ev.clientY - this.disY; if(l < 0){ l = 0; }else if(l > document.documentElement.clientWidth - this.oDiv.offsetWidth){ l = document.documentElement.clientWidth - this.oDiv.offsetWidth; }; if(t < 0){ t = 0; }else if(t > document.documentElement.clientHeight - this.oDiv.offsetHeight){ t = document.documentElement.clientHeight - this.oDiv.offsetHeight; }; this.oDiv.style.left = l + "px"; this.oDiv.style.top = t + "px"; };
相关文章推荐
- 利用架构实例解析面向对象的封装,多态,继承,接口,泛型
- JS基于面向对象实现的拖拽库实例
- 面向对象的分析设计程序思想通过封装、继承、多态将程序耦合度降低,使程序更加灵活、容易修改和易于复用。
- JS基于面向对象实现的拖拽库实例
- 【自】JavaScript面向对象初探二:通过编写一个拖拽例子,让我们了解对象的继承
- 面向对象的继承的实例
- 02-面向对象(继承-子父类中的构造函数-子类的实例化过程-细节) 02-面向对象(继承-子父类中的构造函数-子类的实例化过程-细节)2 02-面向对象(继承-子父类中的构造函数-子类的实例化
- 架构实例解析面向对象的封装,多态,继承,接口,泛型
- c#面向对象特征之继承开发实例
- js面向对象的继承实例-call
- Python面向对象之继承和组合用法实例分析
- 详解JavaScript基于面向对象之继承实例
- js面向对象(json对象、继承、面向对象拖拽)
- [扫盲]利用架构实例解析面向对象的封装,多态,继承,接口,泛型
- 详解JavaScript基于面向对象之继承实例
- Python面向对象——继承 实例
- js面向对象继承(拖拽框)
- Nhibernate+SQLite 入门实例指南二 类的继承、多态关系
- [阅读笔记]面向对象的设计法则--法则1:优先使用(对象)组合,而非(类)继承 Favor Composition Over Inheritance
- 实例解析继承体系重构及ORM映射