JS寫的DIV拖動類

<!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>
<title>JS寫的DIV拖動類_網頁代碼站(www.webdm.cn)</title>
<style type="text/css">
*{ margin:0; padding:0;}
#dragDiv{ width:200px; height:200px; position:absolute; top:100px; left:100px; background:#ddd;}
</style>
</head>
<body>
<div id="dragDiv"></div>
<script type="text/javascript">
var dragDiv=document.getElementById('dragDiv');
//鼠標按下動作
dragDiv.onmousedown=function(event){
var e=event||window.event,
t=e.target||e.srcElement,
//鼠標按下時的座標x1,y1
x1=e.clientX,
y1=e.clientY,
//鼠標按下時的左右偏移量
dragLeft=this.offsetLeft,
dragTop=this.offsetTop;
document.onmousemove=function(event){
var e=event||window.event,
t=e.target||e.srcElement,
//鼠標移動時的動態座標
x2=e.clientX,
y2=e.clientY,
//鼠標移動時的座標的變化量
x=x2-x1,
y=y2-y1;
dragDiv.style.left=(dragLeft+x)+'px';
dragDiv.style.top=(dragTop+y)+'px';
}
document.onmouseup=function(){
this.
}
}
</script>
<br>
<p><a href="http://www.webdm.cn">網頁代碼站</a> - 最專業的網頁代碼下載網站 - 致力爲中國站長提供有質量的網頁代碼!</p>
</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章