日期:2014-05-16 浏览次数:20350 次
<html>
<head>
<script>
var mouseondiv = 0; //如果鼠标移动到图片上方时为1
var mousedrag = 0; //如果鼠标是点中图片并准备拖动为1
var mouseXondiv = 0; //鼠标在图片上的坐标(相对于图片)
var mouseYondiv = 0;
function init(){
document.getElementById("dragdiv").onmouseover=function(){mouseondiv=1;};
document.getElementById("dragdiv").onmouseout=function(){mouseondiv=0;};
document.getElementById("dragdiv").onmousemove = mousemove;
document.getElementById("dragdiv").onmousedown = mousedown;
document.getElementById("dragdiv").onmouseup = mouseup;
document.getElementById("dragdiv").ondragstart = mouseunvalid;
}
function mouseunvalid(){
window.event.returnValue = false;
}
function mouseup(){
mousedrag = 0;
}
function mousedown(){
if(mouseondiv == 1){
mousedrag = 1;
mouseXondiv = event.clientX - parseInt(document.getElementById("dragdiv").style.left);
mouseYondiv = event.clientY - parseInt(document.getElementById("dragdiv").style.top);
} else {
mousedrag = 0;
}
}
function mousemove(){
if(mousedrag == 1){
document.getElementById("dragdiv").style.left = event.clientX - mouseXondiv;
document.getElementById("dragdiv").style.top = event.clientY - mouseYondiv;
}
}
</script>
</head>
<body onload="init()">
<div id="dragdiv" style="HEIGHT: 90px; LEFT: 10px; POSITION: absolute; TOP: 10px; WIDTH: 90px">
<IMG border=3 src="buddies.jpg" >
</div>
</body>
</body>
</html>