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>

你可能感兴趣的:(html,style,div,position)