<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>標(biāo)題頁</title>
<script LANGUAGE="JavaScript">
function DragEvent()
{
//參數(shù)
//x,y 開始時的鼠標(biāo)在對象中的偏移位置
//DargFlag 0:拖曳停止 1:拖曳開始
this.x = 0;
this.y = 0;
this.DragFlag=0;
}
var DragObject = new DragEvent();
function DragMoveObject(obj)
{
if(event.button == 1) //如果按下的是鼠標(biāo)左鍵
{
obj.style.position="absolute"; //設(shè)置對象為絕對定位模式
if(DragObject.DragFlag==0) //拖曳開始
{
DragObject.DragFlag = 1;
DragObject.x = event.offsetX;//鼠標(biāo)的x坐標(biāo)
DragObject.y = event.offsetY;//鼠標(biāo)的y坐標(biāo)
}
obj.style.left = event.x-DragObject.x; //保持鼠標(biāo)在對象中的位置不變
obj.style.top = event.y-DragObject.y;
}
else
{
DragObject.DragFlag = 0; //拖曳停止
}
}
</script>
</head>
<body>
<textarea cols="30" rows="5" onMouseDown="DragMoveObject(this);" ></textarea><br />
<input type=button value="拖拽" onmousedown="DragMoveObject(this);" />
<input id="Button1" type="button" value="button"onmousedown="DragMoveObject(this);" />
</body>
</html>