JavaScript面向对象简单拖拽
2017-03-30 18:31
661 查看
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> #div1{ width: 200px; height: 200px; position: absolute; background: red; } #div2{ width: 200px; height: 200px; position: absolute; background: green; } </style> <script src="面向对象拖拽.js"></script> <script src="限制范围拖拽(继承).js"></script> <script> window.onload=function () { new Drag('div1'); new LimitDrag('div2') } </script> </head> <body> <div id="div1">普通拖拽</div> <div id="div2">限制范围拖拽</div> </body> </html>
//普通拖拽 function Drag(id) { this.oDiv=document.getElementById(id) this.dis_x=0; this.dis_y=0; var _this=this; this.oDiv.onmousedown=function (ev) { _this.fnDown(ev) } } Drag.prototype.fnDown=function (ev) { var oEvent=ev||event; this.dis_x=oEvent.clientX-this.oDiv.offsetLeft; this.dis_y=oEvent.clientY-this.oDiv.offsetTop; var _this=this; document.onmousemove=function (ev) { _this.fnMove(ev) } document.onmouseup=function (ev) { _this.fnUp(); } } Drag.prototype.fnMove=function (ev) { var oEvent=ev||event; this.oDiv.style.left=oEvent.clientX-this.dis_x+'px'; this.oDiv.style.top=oEvent.clientY-this.dis_y+'px'; } Drag.prototype.fnUp=function () { document.onmousedown=null; document.onmousemove=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.dis_x; var t=oEvent.clientY-this.dis_y; 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'; if (t<0) { t=0 } else if (t>document.documentElement.clientH 96c9 eight-this.oDiv.offsetHeight) { t=document.documentElement.clientHeight-this.oDiv.offsetHeight } this.oDiv.style.top=t+'px'; }
相关文章推荐
- JavaScript面向对象简单拖拽
- javascript面向对象简单示例
- Javascript实现最简跨平台面向对象拖拽
- Javascript实现最简跨平台面向对象拖拽
- Javascript实现最简跨平台面向对象拖拽
- JavaScript拖拽(四):面向对象和继承
- 谈谈javascript的面向对象
- Javascript面向对象程序设计培训回顾
- [转]JavaScript简单实现div拖拽
- Javascript的面向对象
- (转)JavaScript 面向对象程序设计(下)——继承与多态
- JavaScript 面向对象程序设计(上)——封装
- javascript面向对象支持
- JavaScript 面向对象程序设计(上)——封装
- JavaScript 面向对象程序设计(上)——封装[转]
- JavaScript 面向对象程序设计(上)--封装
- JavaScript的面向对象机理2)-继承
- JavaScript 面向对象程序设计(下)——继承与多态
- JavaScript中调用ATL双接口简单对象(无界面的AxtiveX控件)
- 面向对象的 Javascript 面向对象基础