<html>
<head>
<script language="javascript">
function Pos(x,y)
{
this.x = x;
this.y = y;
}
var currentMenuItem;
var currentMenuItemPos = new Pos( 0,0 );
function Page_load()
{
document.body.ondragenter = function()
{
window.event.returnValue = false;
}
document.body.ondragover =function()
{
window.event.returnValue = false;
}
//
SetDrogDiv(div1);
}
function SetDrogDiv(objDiv)
{
objDiv.style.position = "absolute"
objDiv.style.width = 100 + "px";
objDiv.style.left = currentMenuItemPos.x +document.body.scrollLeft +"px";
objDiv.style.top = currentMenuItemPos.y + document.body.scrollTop +"px";
//
objDiv.ondragstart = function()
{
currentMenuItemPos = new Pos( event.offsetX,event.offsetY );
}
objDiv.ondrag = function()
{
if(currentMenuItem!=null)
{
this.style.left = parseInt(this.style.left) + event.offsetX- currentMenuItemPos.x;
this.style.top = parseInt(this.style.top) + event.offsetY - currentMenuItemPos.y;
}
}
objDiv.ondragend = function()
{
currentMenuItemPos.x = parseInt(this.style.left) - parseInt(document.body.scrollLeft);
currentMenuItemPos.y = parseInt(this.style.top) - parseInt(document.body.scrollTop);
if(currentMenuItemPos.x < 0 ) //防止将窗口拖出去之后拖不回来
{
currentMenuItemPos.x = 0;
}
if(currentMenuItemPos.y < 0 ) //防止将窗口拖出去之后拖不回来
{
currentMenuItemPos.y = 0;
}
window.event.returnValue = false;
}
//currentMenuItem = objDiv;
//
currentMenuItem = objDiv;
}
</script>
</head>
<body οnlοad="Page_load()">
<div id="div1"οnmοusedοwn="currentMenuItem.dragDrop();">
<a >aaaaaaaa</a><p>sdfsdf<p>拖动
</div>
</body>
</html>