JavaScript拖拽(四):面向对象和继承
来源:互联网 发布:腾讯国际足球数据库 编辑:程序博客网 时间:2024/06/05 02:11
前面的几篇拖拽文章是常规的写法,本文用面向对象的方式重写拖拽,然后继承出限制范围的拖拽。
父类
面向对象,假设构造函数为Drag,继承出的限制范围的拖拽为LimitDrag。
既然面向对象,就得有属性和方法。
属性:要拖拽的div,要保存的距离disX 和 disY;
方法:三个事件–down,move,up;
方法用原型的方式共享。
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>面向对象的拖拽</title> <style type="text/css"> #div1 { width: 100px; height: 100px; background: orange; position: absolute; } </style></head><body> <div id="div1"></div> <script type="text/javascript"> function Drag(id) { var _this = this; this.oDiv = document.getElementById(id); this.disX = 0; this.disY = 0; //this.oDiv.onmousedown = this.fnDown; //上面注释的代码中this.fnDown中的this指的是this.oDiv, //而不是Drag,所以要先把Drag的this保存起来_this this.oDiv.onmousedown = function(ev) { console.log(this);//oDiv _this.fnDown(ev); }; } Drag.prototype.fnDown = function(ev) { var _this = this; var oEvent = ev || event; this.disX = oEvent.clientX - this.oDiv.offsetLeft; this.disY = 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; var l = oEvent.clientX - this.disX; var t = oEvent.clientY - this.disY; this.oDiv.style.cssText = ';left:' + l + 'px;top:' + t + 'px;'; } Drag.prototype.fnUp = function() { document.onmousemove = null; document.onmouseup = null; }; new Drag('div1'); </script></body></html>
继承
属性的继承用call欺骗方式,方法的继承用复制prototype的方式,因为prototype是对象,引用型变量,这里得注意!
Drag.js:
function Drag(id) { var _this = this; this.oDiv = document.getElementById(id); this.disX = 0; this.disY = 0; this.oDiv.onmousedown = function(ev) { console.log(this); _this.fnDown(ev); return false;//阻止浏览器的一些默认行为bug };}Drag.prototype._getStyle = function(obj, attr) { if (obj.currentStyle) { return obj.currentStyle[attr]; } else { return getComputedStyle(obj, null)[attr]; }}Drag.prototype.fnDown = function(ev) { var _this = this; var oEvent = ev || event; this.disX = oEvent.clientX - this.oDiv.offsetLeft; this.disY = 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; var l = oEvent.clientX - this.disX; var t = oEvent.clientY - this.disY; this.oDiv.style.cssText = ';left:' + l + 'px;top:' + t + 'px;';}Drag.prototype.fnUp = function() { document.onmousemove = null; document.onmouseup = null;};
LimitDrag:
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>继承</title> <script type="text/javascript" src="./Drag.js"></script> <style type="text/css"> #div1 { width: 100px; height: 100px; background: orange; position: absolute; } </style></head><body> <div id="div1"></div> <script type="text/javascript"> function LimitDrag(id) { Drag.call(this, id);//除了this,一定要考虑原有的参数 } /* prototype属性是个对象,对象是引用型变量,不会复制,变量名都指向同一块内存地址 LimitDrag.prototype = Drag.prototype;如果这么写的话,两者都指向同一块内存地址 改动了两者都受影响,注意引用类型和基本类型的区别! */ for (var i in Drag.prototype) { LimitDrag.prototype[i] = Drag.prototype[i]; } //重写覆盖父类的fnMove LimitDrag.prototype.fnMove = function(ev) { var oEvent = ev || event; var l = oEvent.clientX - this.disX; var t = oEvent.clientY - this.disY; if (l < 0) { l = 0; } else if (l > document.documentElement.clientWidth - parseInt(this._getStyle(this.oDiv, 'width'))/*this.oDiv.offsetWidth*/) { l = document.documentElement.clientWidth - parseInt(this._getStyle(this.oDiv, 'width'))/*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.cssText = ';left:' + l + 'px;top:' + t +'px;'; }; new LimitDrag('div1'); </script></body></html>
阅读全文
0 0
- JavaScript拖拽(四):面向对象和继承
- javaScript 面向对象-继承(四)
- javascript 面向对象基础(四)面向对象的继承
- javascript面向对象(继承)
- JavaScript面向对象之Prototypes和继承
- Javascript 面向对象,继承和多态
- 面向对象的JavaScript 四 ----- Javascript实现继承的方式(1)
- JavaScript面向对象------继承
- JavaScript面向对象---继承
- javaScript 面向对象-继承
- JavaScript面向对象------继承
- javascript面向对象-继承
- JavaScript面向对象-继承
- javaScript面向对象-继承
- 面向对象(四)——继承
- JavaScript 面向对象(四)call()和apply()
- Javascript面向对象编程-原型式继承和复制继承
- javascript面向对象(四)
- 数组分割
- eclipse中maven项目jar包不会自动下载解决办法
- 2017.5.14测试 第2题: 魔法阵
- selenium使用chrome浏览器
- tensorflow中的关键字global_step使用
- JavaScript拖拽(四):面向对象和继承
- tensorflow的一些代码分析(四) tensorflow常用函数介绍
- BZOJ 1927: [Sdoi2010]星际竞速 费用流
- nginx启动、关闭命令
- 5分钟看懂DNS
- docker 允许主机ssh到docker容器中
- 你应该知道的16个Linux服务器监控命令
- 谷歌“守夜人”:站在互联网黑暗世界,你从来没听说过的一支神秘战队
- Fiddler抓包6-get请求(url详解)