<script type="text/javascript">
window.onload=Drag;
function Drag(){
var oDrag=document.getElementById('one'),
w=document.getElementById('w');
document.onmousedown=fnDown;
}
function fnDown(event){
var oDrag=document.getElementById('one'),
disX=event.clientX-oDrag.offsetLeft,
disY=event.clientY-oDrag.offsetTop;
document.onmousemove=function(event){
event=event||window.event;
fnMove(event,disX,disY);
}
document.onmouseup=function(){
document.onmousedown=null;
document.onmouseup=null;
}
}
function fnMove(event,disX,disY){
var oDrag=document.getElementById('one'),
l=event.clientX-disX,
t=event.clientY-disY;
oDrag.style.left=l+'px';
oDrag.style.top=t+'px';
}
</script>
// 鼠标弹起时不再移动 document.onmouseup = function () { document.onmousemove = null; return false }; // 解决有些时候,在鼠标松开的时候,元素仍然可以拖动; document.ondragstart = function (ev) { ev.preventDefault(); console.log(1212) }; document.ondragend = function (ev) { ev.preventDefault(); };
楼上的一样,事件设置为null了
document.onmouseup=function(){ document.onmousemove=null; document.onmouseup=null; }