js 实现拖动层效果


<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>hello</title>
</head>
<script >

/* 以下代码支持使层可以拖动***********Begin******************/
 
   function $(_sId){return document.getElementById(_sId);}
   
   function moveStart (event, _sId)
   {
    var oObj = $(_sId);
     
    oObj.onmousemove = mousemove;
    oObj.onmouseup = 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.onmousemove = null;
      oObj.onmouseup = 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(){ document.getElementById('div1').contentEditable = true;};
   }
  } 
  
/* 以上代码支持使层可以拖动***********End******************/ 

  function onDataChanged(obj)
  { 
       var objParent =  obj.parentNode;
      var childElement = objParent.firstChild; 
     childElement.value = obj.innerText ;
    
     if(event.keyCode == 13)
       {      
         var txtobj=document.selection.createRange();
  txtobj.text==""?txtobj.text="\n":(document.selection.clear())&(txtobj.text="\n"); //三目复合表达式,解决有被选文字时回车的光标定位问题 
  document.selection.createRange().select(); 
  return false ; 
      }

  }
</script>
<body οnlοad="onDataChanged(document.getElementById('div1'));">

<div name="divTextAreaContainer" id="divTextAreaContainer" style="position:absolute;border:0px solid #C0C0C0;width:552px;left:0px;top:0px;"  οnmοusedοwn="moveStart(event,'divTextAreaContainer')"   > 
<input type="hidden" name="hidden@id@" id="hidden@id@" value="" />
<div id="div1" style="border:1px solid #C0C0C0;WORD-BREAK: break-all; WORD-WRAP: break-word;overflow:scroll;height:357px;width:552px; top:20px;left:100px"    
 οnclick="this.contentEditable= true;"  οnkeyup="onDataChanged(this)">hello 
</div> 
</div> 
</body>

</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值