javascript 鼠標拖動功能

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GRAG--彈出框顯示居中並能隨意拖動</title>
<script type="text/javascript" src="jQuery.js"></script>
</head>

<body>
<style type="text/css">
#slideBtn,#dragBox{cursor:pointer;text-align:center;color:#FA0;}
#slideBtn{width:100px; height:24px; line-height:24px; border:#FA0 1px solid; padding:5px 0; margin:10px;}
#dragBox{ width:200px; height:200px;border:#F00 1px solid; background:#999; position:absolute; display:none; z-index:10;font-size:20px; color:#960; line-height:160px;}
#close{ text-align:right;cursor:pointer; margin:5px; height:15px; line-height:15px;}
</style>
<div id="slideBtn">點擊顯示</div>

<div id="dragBox">
    <div id="close" onclick="$('#dragBox').fadeOut();">關閉</div>
    這個是可以拖動的框
</div>


<script type="text/javascript">
var a,
    b,
    c,
    d,
    f,
    g,
    h,
    j = $(window),
    n = $(document).height() - 202,
    o = j.width() - 202,
    p = (j.height() -202) / 2 + j.scrollTop(),
    q = o /2,
    R = $('#dragBox');
$("#slideBtn").click(function(){
    if(R.css('display')=='none'){
        posi();
        R.slideDown();
    }else{
        R.fadeOut();
    } 
});
function posi(){
    R.css({'top':p+'px','left':q+'px'});
}
R.mousedown(function(e){
    a = parseInt($(this).css('top'),10);
    b = parseInt($(this).css('left'),10);
    c = a - e.clientY;
    d = b - e.clientX;
    h = true;
})
$(document).mousemove(function(e){
    if(!h){return false;}
    e = e || window.event;
    e.preventDefault();
    e.returnValue=false;
    f = e.clientY +c;
    g = e.clientX +d;
    f = f < 0 ? 0 : (f> n ? n : f);
    g = g < 0 ? 0 : (g> o ? o : g);
    R.css({'top':f+'px','left':g+'px'});
});
$(document).mouseup(function(e){h = false;});
</script>
</body>
</html>

下面是純js實現方式

function $D(id){return document.getElementById(id);}
function BindAsEventListener(object,fun){
    return function(event){
        return fun.call(object,(event || window.event));
    }
}
function Bind(object,fun){
    return function(){
        return fun.apply(object,arguments);
    }
}
function addEvent(target,type,fun){
    if(target.addEventListener){
        target.addEventListener(type,fun,false);
    }else if(target.attachEvent){
        target.attachEvent('on'+type,fun);
    }else{
        target['on'+tyle] = null;
    }
}
function removeEvent(target,type,fun){
    if(target.removeEventListener){
        target.removeEventListener(type,fun,false);
    }else if(target.detachEvent){
        target.detachEvent('on'+type,fun);
    }else{
        target['on'+type] = null;
    }
    
}

function getStyle(elem,attr){ 
    if(elem.attr){  
        return elem.style[attr]; 
    }else if(elem.currentStyle){ 
        return elem.currentStyle[attr]; 
    }else if(document.defaultView && document.defaultView.getComputedStyle){ 
        attr=attr.replace(/([A-Z])/g,'-$1').toLowerCase(); 
        return document.defaultView.getComputedStyle(elem,null).getPropertyValue(attr); 
    }else{ 
        return null; 
    } 
} 

var Drag = function(){this.init.apply(this, arguments);}
Drag.prototype = {
    init: function(d){
            this.Drag = $D(d);
            this._x = this._y = this._x1 = this._y1 = 0;
            this._dh = document.documentElement.scrollHeight - parseInt(getStyle(this.Drag,'height'),10) - 2;
            this._ww = (window.innerWidth - parseInt(getStyle(this.Drag,'width'),10)) - 2;//2為邊框
        this._fM = BindAsEventListener(this, this.Move);
            this._fS = Bind(this,this.Stop);
            this.Drag.style.position = 'absolute';
            addEvent(this.Drag, 'mousedown', BindAsEventListener(this,this.Star));
    },
    Star: function(e){
            this._x = e.clientX - this.Drag.offsetLeft;
            this._y = e.clientY - this.Drag.offsetTop;
            addEvent(document, 'mousemove', this._fM);
            addEvent(document, 'mouseup', this._fS);
    },
    Move: function(e){
            this.Rang(e.clientX - this._x,e.clientY - this._y);
            this.Drag.style.left = this._x1 + 'px';
            this.Drag.style.top = this._y1 + 'px';
    },
    Stop: function(e){
            removeEvent(document, 'mousemove', this._fM);
            removeEvent(document, 'mouseup', this._fS);
    },
    Rang: function(x,y){
            this._x1 = x < 0 ? 0 : ( x > this._ww ? this._ww : x );
            this._y1 = y < 0 ? 0 : ( y > this._dh ? this._dh : y );
    }
};

new Drag('dragBox');
原文地址:https://www.cnblogs.com/helin/p/3249719.html