javascript面向对象系列讲解之拖拽(继承知识)实战



javascript面向对象系列讲解之拖拽(继承知识)实战  。声明:为了方便大家学习和查看,所以特意控制了文章的篇幅,将面向对象写成了多篇连续博文的方式,也方便大家根据自己具体情况进行选择性的学习

在上一篇文章当中,我们讲解了面向对象的Tab切换书写方法,本文当中,我将为大家介绍拖拽的面向对象写法以及继承的基本知识。

首先,我们来书写一个拖拽的基本功能代码,在此就不贴代码了,直接上代码截图(毕竟这个原版的拖拽不是我们这次要讲解的重点)

面向对象系列讲解——拖拽(继承知识)实战 - 独行冰海 - 独行冰海

 

注意:这个拖拽是没有条件限制的拖拽

接下来按照我们上一篇文章中讲过的处理方法进行处理:

将函数进行提取,不允许出现嵌套的函数;修改为构造函数,为属性添加“this.”,把方法提取出来,利用原型进行书写。

提示一下,在代码的书写过程中,要需要注意两点,防止出现问题。

第一,this的使用

第二,在一个函数当中进行另一个函数的调用时,记得使用function(){}进行包装


成品代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset=”utf-8″/>
  5.     <title>拖拽div-面向对象-独行冰海</title>
  6.     <meta name=”keywords” content=”"/>
  7.     <meta name=”description” content=”"/>
  8. <style type=”text/css”>
  9.     #warp{
  10.         border:8px yellow double;
  11.         width:400px;height:400px;
  12.         background:#ccc;
  13.         position:relative;
  14.         }
  15.     #sport{
  16.         border:5px yellow double;
  17.         width:50px;height:50px;
  18.         background:blue;
  19.         position:absolute;
  20.         top:0;left:0;
  21.         }
  22. </style>
  23. </head>
  24. <body>
  25.     <div id=”warp”>
  26.         <div id=”sport”>没有边界的拖拽-独行冰海</div>
  27.     </div>
  28. <script type=”text/javascript”>
  29.     function Drag(id){
  30.         var _this = this;
  31.         this.sportX = null;
  32.         this.sportY = null;
  33.         this.sport = document.getElementById(id);
  34.         this.sport.onmousedown = function(event){
  35.             _this.dragStart(event);
  36.         }
  37.     }
  38.     Drag.prototype.dragStart = function(event){
  39.         var _this = this;
  40.         var event = event || window.event;
  41.         this.sportX = this.sport.offsetLeft – event.clientX;
  42.         this.sportY = this.sport.offsetTop – event.clientY;
  43.         event.preventDefault();
  44.         document.onmousemove = function(event){
  45.             _this.dragMove(event);
  46.         }
  47.         document.onmouseup = _this.dragEnd;
  48.     }
  49.     Drag.prototype.dragMove = function(event){
  50.         var event = event || window.event;
  51.         this.sport.style.left = this.sportX + event.clientX + ‘px’;
  52.         this.sport.style.top = this.sportY + event.clientY + ‘px’;
  53.         event.preventDefault();
  54.     }
  55.     Drag.prototype.dragEnd = function(){
  56.         document.onmousemove = null;
  57.     }
  58.     new Drag(‘sport’);
  59. </script>
  60. </body>
  61. </html>

接下来说说用继承实现限制的拖拽:

当我们已经能够实现没有限制的拖拽的时候,如果需要我们实现有边界的限制拖拽,相信很少有人愿意从头到尾再重新书写一个。于是,就有了所谓的“继承”。从“父级”将父级的功能全部获取下来,然后稍加改动,就形成了拥有更强大功能的函数。

那么如何实现有限制的拖拽效果呢,使用继承的几个基本步骤又是什么呢?

1、利用call的方法进行属性的继承;

2、利用for-in循环的方式继承原来的所有方法;

3、重新书写和上面不同的内容。

具体代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset=”utf-8″/>
  5.     <title>拖拽div-面向对象-独行冰海</title>
  6.     <meta name=”keywords” content=”"/>
  7.     <meta name=”description” content=”"/>
  8. <style type=”text/css”>
  9.     #warp{
  10.         border:8px yellow double;
  11.         width:400px;height:400px;
  12.         background:#ccc;
  13.         position:relative;
  14.     }
  15.     #sport{
  16.         position:absolute;
  17.         top:0;left:0;
  18.         width:50px;height:50px;
  19.         border:5px yellow double;
  20.         background:blue;
  21.     }
  22.     #limitsport{
  23.         position:absolute;
  24.         top:0;left:0;
  25.         width:50px;height:50px;
  26.         border:5px yellow double;
  27.         background:#cfc;
  28.     }
  29. </style>
  30. </head>
  31. <body>
  32.     <div id=”warp”>
  33.         <div id=”sport”>普通拖拽-独行冰海</div>
  34.         <div id=’limitsport’>限制拖拽-独行冰海</div>
  35.     </div>
  36. <script type=”text/javascript”>
  37.     function Drag(id){
  38.         var _this = this;
  39.         this.sportX = null;
  40.         this.sportY = null;
  41.         this.sport = document.getElementById(id);
  42.         this.sport.onmousedown = function(event){
  43.             _this.dragStart(event);
  44.         }
  45.     }
  46.     Drag.prototype.dragStart = function(event){
  47.         var _this = this;
  48.         var event = event || window.event;
  49.         this.sportX = this.sport.offsetLeft – event.clientX;
  50.         this.sportY = this.sport.offsetTop – event.clientY;
  51.         event.preventDefault();
  52.         document.onmousemove = function(event){
  53.             _this.dragMove(event);
  54.         }
  55.         document.onmouseup = _this.dragEnd;
  56.     }
  57.     Drag.prototype.dragMove = function(event){
  58.         var event = event || window.event;
  59.         this.sport.style.left = this.sportX + event.clientX + ‘px’;
  60.         this.sport.style.top = this.sportY + event.clientY + ‘px’;
  61.         event.preventDefault();
  62.     }
  63.     Drag.prototype.dragEnd = function(){
  64.         document.onmousemove = null;
  65.     }
  66.     new Drag(‘sport’);
  67.     // 需要进行继承
  68.     // 创建构造
  69.     function LimitDrag(id, limId){
  70.         Drag.call(this, id);    // 继承属性
  71.         this.warp = document.getElementById(limId);
  72.     }
  73.     // 继承方法
  74.     for(var i in Drag.prototype){
  75.         LimitDrag.prototype[i] = Drag.prototype[i];
  76.     }
  77.     new LimitDrag(‘limitsport’, ‘warp’);
  78.     // 在基础上面进行不同内容的重新书写,即DragMove方法
  79.     LimitDrag.prototype.dragMove = function(){
  80.         var event = event || window.event;
  81.         var nowX = this.sportX + event.clientX;
  82.         var nowY = this.sportY + event.clientY;
  83.         var redconX = this.warp.clientWidth – this.sport.offsetWidth;
  84.         var redconY = this.warp.clientHeight – this.sport.offsetHeight;
  85.         if(nowX >= redconX){
  86.             nowX = redconX;
  87.         }
  88.         if(nowX <= 0){
  89.             nowX = 0;
  90.         }
  91.         if(nowY >= redconY){
  92.             nowY = redconY;
  93.         }
  94.         if(nowY <= 0){
  95.             nowY = 0;
  96.         }
  97.         this.sport.style.left = nowX + ‘px’;
  98.         this.sport.style.top = nowY + ‘px’;
  99.         event.preventDefault();
  100.     }
  101. </script>
  102. </body>
  103. </html>

到此为止,面向对象系列的基本知识就全部讲完了,感谢大家的支持。