拖动问题,好奇怪啊!!!
html代码如下:
<html>
<head>
<title>Show Mouse Coordinates</title>
<script language="javascript">
document.onmousemove = mouseMove;
document.onmouseup = mouseUp;
var dragObject = null;
var mouseOffset = null;
function getMouseOffset(target, ev){
ev = ev || window.event;
var docPos = getPosition(target);
var mousePos = mouseCoords(ev);
return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}
function getPosition(e){
var left = 0;
var top = 0;
while (e.offsetParent){
left += e.offsetLeft;
top += e.offsetTop;
e = e.offsetParent;
}
left += e.offsetLeft;
top += e.offsetTop;
return {x:left, y:top};
}
function mouseMove(ev){
ev = ev || window.event;
var mousePos = mouseCoords(ev);
if(dragObject){
dragObject.style.position = 'absolute';
dragObject.style.top = mousePos.y - mouseOffset.y;
dragObject.style.left = mousePos.x - mouseOffset.x;
return false;
}
}
function mouseUp(){
dragObject = null;
}
function makeDraggable(item){
if(!item) return;
item.onmousedown = function(ev){
dragObject = this;
mouseOffset = getMouseOffset(this, ev);
return false;
}
}
function mouseCoords(ev){
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
</script>
</head>
<body>
<div onmousedown="makeDraggable(this)" onmouseup="mouseUp();" onmousemove="mouseMove(this)" id="club">
<img src="club.gif" />
</div>
</div>
</body>
</html>
问题是:当页面刷新第一次拖动的时候,不行!
当第二次拖动的时候,画面上的草花就可以动了!不明白为什么,问一下!
------解决方案--------------------
HTML code
<div onmousedown="makeDraggable(event,this)" onmouseup="mouseUp();" onmousemove="mouseMove(this)" id="club">
<img src="1.gif" />
</div>