码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象的拖拽

时间:2015-04-18 17:18:31      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Examples</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <link href="" rel="stylesheet">
    <style>
    #div1 {
        width: 100px;
        height: 100px;
        background: red;
        position: absolute;
    }
    </style>
</head>

<body>
    <div id="div1"></div>
    <script>

    // var oDiv1 = document.getElementById("div1");
    // oDiv1.onmousedown = function(event) {
    //     var disX = event.clientX - oDiv1.offsetLeft;
    //     var disY = event.clientY - oDiv1.offsetTop;
    //     document.onmousemove = function(event) {
    //         oDiv1.style.left = event.clientX - disX + "px";
    //         oDiv1.style.top = event.clientY - disY + "px";
    //     }
    //     document.onmouseup = function() {
    //         document.onmousemove = null;
    //         document.onmouseup = null;
    //     }
    // }
    function Drag(id){
        this.obj=document.getElementById(id);
    }
    Drag.prototype.init=function(){
        var This=this;
        this.obj.onmousedown=function(event){
            This.down(event);
            document.onmousemove =function(event){
                This.move(event)
            }
            document.onmouseup = function() {
                This.up()
            }
            return false;
        }
    }
    Drag.prototype.down=function(event){
        this.disX=event.clientX - this.obj.offsetLeft;
        this.disY=event.clientY - this.obj.offsetTop;  
    }
    Drag.prototype.move=function(event){
        this.obj.style.left = event.clientX - this.disX + "px";
        this.obj.style.top = event.clientY - this.disY + "px";
    }
    Drag.prototype.up=function(event){
        document.onmousemove = null;
        document.onmouseup = null;
    }

    var drag1=new Drag("div1")
    drag1.init();
    </script>
</body>

</html>

 

面向对象的拖拽

标签:

原文地址:http://www.cnblogs.com/liujin0505/p/4437514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!