面向对象编程——案例篇(拖拽)

原始的面向过程代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <style>
            #box {
                width: 100px; 
                height: 100px; 
                background: blue; 
                position: absolute;
            }
        </style>
        <title>拖拽</title>
        <script>
            var oBox=null;
            var disX=0;
            var disY=0;
            
            window.onload=function(){
                oBox=document.getElementById('box');
                
                oBox.onmousedown=fnDown;
            };
            //鼠标按下事件
            function fnDown(ev){
                var oEvent = ev||event;
                disX = oEvent.clientX - oBox.offsetLeft;
                disY = oEvent.clientY - oBox.offsetTop;
                
                document.onmousemove = fnMove;
                document.onmouseup = fnUp;
            }
            //鼠标移动事件
            function fnMove(ev){
                var oEvent=ev||event;
                
                oBox.style.left = oEvent.clientX - disX + 'px';
                oBox.style.top = oEvent.clientY - disY + 'px';
            }
            //鼠标抬起事件
            function fnUp(){
                document.onmousemove = null;
                document.onmouseup = null;
            }
        </script>
    </head>

<body>
    <div id="box"></div>
</body>
</html>

下面是面向对象的代码

drag.js

/**
 * 拖拽
 * @param {Object} id div的id
 */
function Drag(id){
    this.oBox = document.getElementById(id);
    this.disX = 0;
    this.disY = 0;
    
    var _this = this;
    
    this.oBox.onmousedown = function(){
        _this.fnDown();
    }
}
//鼠标按下
Drag.prototype.fnDown = function(ev){
    var oEvent = ev || event;
    
    this.disX = oEvent.clientX - this.oBox.offsetLeft;
    this.disY = oEvent.clientY - this.oBox.offsetTop;
    
    var _this = this;
    
    document.onmousemove = function(){
        _this.fnMove();
    };
    document.onmouseup = function(){
        _this.fnUp();
    };
}
//鼠标移动
Drag.prototype.fnMove = function(ev){
    var oEvent= ev || event;
    
    this.oBox.style.left = oEvent.clientX - this.disX + 'px';
    this.oBox.style.top = oEvent.clientY - this.disY + 'px';
}
//鼠标抬起
Drag.prototype.fnUp = function(){
    document.onmousemove = null;
    document.onmouseup = null;
}

drag.html 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <style>
            div {
                position: absolute;
            }
        </style>
        <title>拖拽</title>
        <script type="text/javascript" src="../js/drag.js" ></script>
        <script>
            window.onload = function(){
                var drag1 = new Drag("box1");
                
                var drag1 = new Drag("box2");
            };
        </script>
    </head>

<body>
    <div id="box1" style="background: red;width: 200px;height: 200px;"></div>    
    <div id="box2" style="background: blue;width: 100px;height: 100px;"></div>
</body>
</html>

 

 

此拖拽有一个问题,就是没有控制拖拽出边界的问题。但我们又不想去修改代码,那我们怎么做?学过java的应该都知道可以写一个子类来做一些更加具体的操作,又保留了父类的功能,就是继承。

html

<script type="text/javascript" src="../js/drag.js" ></script>
<script type="text/javascript" src="../js/dragLimit.js" ></script>
<script>
    window.onload = function(){
          var drag1 = new Drag("box1");              
          var drag1 = new DragLimit("box2");//蓝色是不会超出边界的
    };
</script>
<body>
    <div id="box1" style="background: red;width: 200px;height: 200px;"></div>    
    <div id="box2" style="background: blue;width: 100px;height: 300px;"></div>
</body>

DragLimit.js:DragLimit继承自Drag,控制了不能出边界

/**
 * 限制边界的拖拽,继承自Drag
 * @param {Object} id
 */
function DragLimit(id){
    Drag.call(this, id);
}
//继承方法
for(var p in Drag.prototype){
    DragLimit.prototype[p] = Drag.prototype[p];
}
/**
 * 覆写父类的鼠标移动方法,控制不能移出边界
 */
DragLimit.prototype.fnMove = function(ev){
    var oEvent= ev || event;
    
    var left = oEvent.clientX - this.disX;
    var top = oEvent.clientY - this.disY;
    
    //控制边界
    if(left < 0){
        left = 0;
    } else if(left > document.documentElement.clientWidth-this.oBox.offsetWidth){
        left = document.documentElement.clientWidth-this.oBox.offsetWidth;
    }
    if(top <= 0){
        top = 0;
    } else if(top > document.documentElement.clientHeight-this.oBox.offsetHeight){
        top = document.documentElement.clientHeight-this.oBox.offsetHeight;
    }
    
    this.oBox.style.left = left + 'px';
    this.oBox.style.top = top + 'px';
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值