js开发:通过面向对象方式完成的拖拽功能

研究了两天的JS面向对象了,然后根据视频完成了一个简单的拖拽功能。下面的代码其实很简单,唯一需要注意的就是 this ,感觉JS中的this还是很奇妙很有意思的,接下来学习就研究this指针好啦 。


下面是拖拽的代码

Drag.js

[javascript]  view plain  copy
  1. function Drag(id){  
  2.   
  3.     var _this = this;  
  4.   
  5.     this.divX = 0;  
  6.     this.divY = 0;  
  7.   
  8.     this.oDiv = document.getElementById(id);  
  9.   
  10.     this.oDiv.onmousedown = function(ev){  
  11.         _this.fnMouseDown(ev);  
  12.     }  
  13. }  
  14.   
  15. Drag.prototype.fnMouseDown = function(ev){  
  16.   
  17.     var _this = this;  
  18.   
  19.     var oEvent = ev||event;  
  20.   
  21.     this.divX = oEvent.clientX - this.oDiv.offsetLeft;  
  22.     this.divY = oEvent.clientY - this.oDiv.offsetTop;  
  23.   
  24.     document.onmousemove = function(ev){  
  25.         _this.fnMouseMove(ev);  
  26.   
  27.         return false;//取消选中  
  28.     };  
  29.   
  30.     document.onmouseup = function(){  
  31.         _this.fnMouseUp();  
  32.     }  
  33.   
  34. }  
  35.   
  36. Drag.prototype.fnMouseMove = function(ev){  
  37.     var oEvent = ev||event;  
  38.   
  39.     this.oDiv.style.left = oEvent.clientX - this.divX+'px';  
  40.     this.oDiv.style.top = oEvent.clientY- this.divY+'px';  
  41. }  
  42.   
  43. Drag.prototype.fnMouseUp = function(){  
  44.     document.onmousemove = null;  
  45.     document.onmousedown = null;  
  46. }  

LimitDrag.js //限制范围的拖拽

[javascript]  view plain  copy
  1. /** 
  2.  * Created by Administrator on 2015/12/29 0029. 
  3.  */  
  4. function LimitDrag (id){  
  5.     Drag.call(this,id);  
  6. }  
  7.   
  8. for(var i in Drag.prototype){  
  9.     LimitDrag.prototype[i] = Drag.prototype[i];  
  10. }  
  11.   
  12. //重写 MouseMove 函数  
  13.   
  14. LimitDrag.prototype.fnMouseMove = function(ev){  
  15.   
  16.     var oEvent = ev||event;  
  17.   
  18.     var left = oEvent.clientX - this.divX;  
  19.     var top = oEvent.clientY - this.divY  
  20.   
  21.     if(left<0){  
  22.         left = 0;  
  23.     }else if(left >document.documentElement.clientWidth - this.oDiv.offsetWidth){  
  24.         left= document.documentElement.clientWidth - this.oDiv.offsetWidth;  
  25.     }  
  26.   
  27.     if(top<0){  
  28.         top = 0;  
  29.     }else if(top > document.documentElement.clientHeight - this.oDiv.offsetHeight){  
  30.         top =  document.documentElement.clientHeight - this.oDiv.offsetHeight  
  31.     }  
  32.   
  33.     this.oDiv.style.left = left+'px';  
  34.     this.oDiv.style.top = top+'px';  
  35.   
  36. }  

HTML

[html]  view plain  copy
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>Title</title>  
  6.     <script src="js/Drag.js"></script>  
  7.     <script src="js/LimitDrag.js"></script>  
  8.     <script>  
  9.   
  10.         window.onload = function () {  
  11.             new Drag('div1');  
  12.             new LimitDrag('div2')  
  13.         }  
  14.   
  15.     </script>  
  16.     <style>  
  17.         #div1{  
  18.              width: 200px;  
  19.              height: 200px;  
  20.              background-color: yellow;  
  21.              position: absolute;  
  22.          }  
  23.   
  24.         #div2{  
  25.             width: 200px;  
  26.             height: 200px;  
  27.             background-color: blue;  
  28.             position: absolute;  
  29.         }  
  30.   
  31.     </style>  
  32. </head>  
  33. <body>  
  34.   
  35.     <div id="div1">Drag</div>  
  36.     <div id="div2">LimitDrag</div>  
  37.   
  38. </body>  
  39. </html>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值