日期:2014-05-17 浏览次数:20840 次
为什么只能拖拽一次,第二次会回初始位置。从左上角拖至图片位置:
<html>
<head>
<title> New Document </title>
<style type="text/css">
body{margin:0}
.drag{width:40px;height:40px;background:#123321;position:relative}
</style>
</head>
<body>
<div id="drag" class="drag" onmousedown="move(event)" onmousemove="upmove(event)" onmouseup="stopdrag(event)"></div>
<script type="text/javascript">
var x,y,cx,cy;
var down=false;
function move(event){
var even=event?event:window.event;
var drag=document.getElementById("drag");
x=drag.offsetLeft;
y=drag.offsetTop;
cx=even.clientX;
cy=even.clientY;
down=true;
}
var cx1,cy1;
function upmove(event){
var even=event?event:window.event;
var drag=document.getElementById("drag");
if(down){
cx1=even.clientX-cx;
cy1=even.clientY-cy;
drag.style.left=cx1+"px";
drag.style.top=cy1+"px";
}
}
function stopdrag(event){
var even=event?event:window.event;
var drag=document.getElementById("drag");
down=false;
drag.id="drag";
drag.style.left=cx1+"px";
drag.style.top=cy1+"px";
}
</script>
</body>
</html>
<html>
<head>
<title> New Document </title>
<style type="text/css">
body{margin:0}
.drag{width:40px;height:40px;background:#123321;position:relative}
</style>
</head>
<body>
<div id="drag" class="drag" onmousedown="move(event)" onmousemove="upmove(event)" onmouseup="stopdrag(event)"></div>
<script type="text/javascript">
var x,y,cx,cy;
var down=false;
var time=0;
function move(event){
var even=event?event:window.event;
var drag=document.getElementById("drag");
x=drag.offsetLeft;
y=drag.offsetTop;
cx=ev