JS拖拽代碼兼容FF IE

<html>

<body>

<div id="one" style="position:absolute;left:100px;top:100px;width:200px;height:200px;background-color:red"></div>

<script>
var one=document.getElementById("one");
var down=false;
var dx=0;
var dy=0;
var stylex=0;
var styley=0;


document.onmousemove=function(e){
  var ev=e || window.event;
  if(down){
    one.style.top=ev.clientY-(dy-styley);
    one.style.left=ev.clientX-(dx-stylex);    
  }
}
//鼠標點擊DIV dx:點擊X dy:點擊Y
one.onmousedown=function(e){
  var ev=e || window.event;
  dx=ev.clientX;
  dy=ev.clientY;
  stylex=parseInt(one.style.left);
  styley=parseInt(one.style.top);
  if(!down)
    down=true;
}
//鼠標釋放
document.onmouseup=function(e){
  var ev=e || window.event;
  if(down)
    down=false;
}
</script>

</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章