一个简单的js拖拽

window.onload =function(){
    var oDiv = document.getElementById('div1');
    oDiv.onmousedown= function(ev) {
       var oEvent = ev ||event;
       var divX = oEvent.clientX - oDiv.offsetLeft;
       var divY = oEvent.clientY-oDiv.offsetTop;
       document.onmousemove = function(ev){
        var boxEvent = ev || event;
        var boxX = boxEvent.clientX-divX;
        var boxY = boxEvent.clientY-divY;
        oDiv.style.left = boxX+'px';
        oDiv.style.top = boxY+'px';
       }
       document.onmouseup = function(){
        document.onmousemove=null;
         document.onmouseup = null;
       }
    };
}

 

posted @ 2015-06-26 11:04 前行者在此 阅读(...) 评论(...) 编辑 收藏