|
代碼
復(fù)制代碼 代碼如下:
<body>
<div id="block" style="width:100px; height:100px; position:relative; background-color:#FF0000";></div>
<script>
var bb = document.getElementById("block");
bb.onmousedown = function(){
//獲取鼠標(biāo)當(dāng)前坐標(biāo)
var pageX = event.clientX;
var pageY = event.clientY;
//獲取block的坐標(biāo),左邊界和上邊界
var offX = parseInt(this.style.left)||0;
var offY = parseInt(this.style.top)||0;
//計算出鼠標(biāo)坐標(biāo)相對于block坐標(biāo)的間距
var offLX = pageX-offX;
var offLY = pageY-offY;
if(!document.onmousemove){
document.onmousemove = function(){
bb.style.left=event.clientX-offLX; //設(shè)置block的X坐標(biāo)
bb.style.top=event.clientY-offLY; //設(shè)置block的Y坐標(biāo)
}
}
}
document.onmouseup = function(){document.onmousemove = null;} //鼠標(biāo)彈起
</script>
</body>
JavaScript技術(shù):javascript 鼠標(biāo)拖動圖標(biāo)技術(shù),轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。