转JS的拖动层

一直用自己写的拖动层代码,可是发现只能拖动img等对象,今天憋不住了,网上找了一个,还不错
 
<script language="javascript">
  function $(_sId){return document.getElementById(_sId);}
 
 function moveStart (event, _sId){
   var oObj = $(_sId).parentNode.parentNode;
   oObj. = mousemove;
   oObj. = mouseup;
   oObj.setCapture ? oObj.setCapture() : function(){};
   oEvent = window.event ? window.event : event;
   var dragData = {x : oEvent.clientX, y : oEvent.clientY};
   var backData = {x : parseInt(oObj.style.top), y : parseInt(oObj.style.left)};
  
   function mousemove(){
    var oEvent = window.event ? window.event : event;
    var iLeft = oEvent.clientX - dragData["x"] + parseInt(oObj.style.left);
    var iTop = oEvent.clientY - dragData["y"] + parseInt(oObj.style.top);
    oObj.style.left = iLeft;
    oObj.style.top = iTop;
    dragData = {x: oEvent.clientX, y: oEvent.clientY}; 
   }
  
   function mouseup(){
    var oEvent = window.event ? window.event : event;
    oObj. = null;
    oObj. = null;
    if(oEvent.clientX < 1 || oEvent.clientY < 1 || oEvent.clientX > document.body.clientWidth || oEvent.clientY > document.body.clientHeight){
    oObj.style.left = backData.y;
    oObj.style.top = backData.x;
     }
     oObj.releaseCapture ? oObj.releaseCapture() : function(){};
   }
 }
</script>
<div style="background:#cccccc;width:300px;position:absolute;top:20;left:30">asdcZd<div><div id="mov" style="position:absolute;height:50px;width:300px;background-color:#FF33ff;top:20px;left:100px;" onMouseDown="javascript:moveStart(event,'mov');"></div></div></div>

你可能感兴趣的:(js,职场,拖动,层,休闲)