Javascript 元素拖曳操作

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns=" http://www.w3.org/1999/xhtml">
  3. <!-- DW6 -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>shawl.qiu template</title>
  7. <script type="text/javascript">
  8. //<![CDATA[
  9.     function fDragging(obj, e, limit){
  10.         if(!e) e=window.event;
  11.         var x=parseInt(obj.style.left);
  12.         var y=parseInt(obj.style.top);
  13.         
  14.         var x_=e.clientX-x;
  15.         var y_=e.clientY-y;
  16.         
  17.         if(document.addEventListener){
  18.             document.addEventListener('mousemove', inFmove, true);
  19.             document.addEventListener('mouseup', inFup, true);
  20.         } else if(document.attachEvent){
  21.             document.attachEvent('onmousemove', inFmove);
  22.             document.attachEvent('onmouseup', inFup);
  23.         }
  24.         
  25.         inFstop(e);    
  26.         inFabort(e)
  27.         
  28.         function inFmove(e){
  29.             var evt;
  30.             if(!e)e=window.event;
  31.             
  32.             if(limit){
  33.                 var op=obj.parentNode;
  34.                 var opX=parseInt(op.style.left);
  35.                 var opY=parseInt(op.style.top);
  36.                 
  37.                 if((e.clientX-x_)<0) return false;
  38.                 else if((e.clientX-x_+obj.offsetWidth+opX)>(opX+op.offsetWidth)) return false;
  39.                 
  40.                 if(e.clientY-y_<0) return false;
  41.                 else if((e.clientY-y_+obj.offsetHeight+opY)>(opY+op.offsetHeight)) return false;
  42.                 //status=e.clientY-y_;
  43.             }
  44.             
  45.             obj.style.left=e.clientX-x_+'px';
  46.             obj.style.top=e.clientY-y_+'px';
  47.             
  48.             inFstop(e);
  49.         } // shawl.qiu script
  50.         function inFup(e){
  51.             var evt;
  52.             if(!e)e=window.event;
  53.             
  54.             if(document.removeEventListener){
  55.                 document.removeEventListener('mousemove', inFmove, true);
  56.                 document.removeEventListener('mouseup', inFup, true);
  57.             } else if(document.detachEvent){
  58.                 document.detachEvent('onmousemove', inFmove);
  59.                 document.detachEvent('onmouseup', inFup);
  60.             }
  61.             
  62.             inFstop(e);
  63.         } // shawl.qiu script
  64.  
  65.         function inFstop(e){
  66.             if(e.stopPropagation) return e.stopPropagation();
  67.             else return e.cancelBubble=true;            
  68.         } // shawl.qiu script
  69.         function inFabort(e){
  70.             if(e.preventDefault) return e.preventDefault();
  71.             else return e.returnValue=false;
  72.         } // shawl.qiu script
  73.     }
  74. //]]>
  75. </script>
  76. </head>
  77. <body>
  78. <div  style=" border:1px dashed blue; width: 760px; height:600px;  text-align:center; position:absolute; left:100px; top: 10px;"> this parent
  79.  
  80.     <div style=" border:1px dashed blue; width: 180px; text-align:center; position:absolute; left:50px; top: 50px;" οnmοusedοwn="fDragging(this, event, true);">
  81.         element <br/>
  82.         dragging compatibility for IE, Opera, Firefox. 
  83.     </div>
  84.     <div style=" border:1px dashed blue; width: 180px; text-align:center; position:absolute; left:100px; top: 150px;" οnmοusedοwn="fDragging(this, event, true);">
  85.         element 1<br/>
  86.         dragging compatibility for IE, Opera, Firefox. 
  87.     </div>
  88.     <div style=" border:1px dashed blue; width: 180px; text-align:center; position:absolute; left:200px; top: 250px;" οnmοusedοwn="fDragging(this, event, false);">
  89.         element 2<br/>
  90.         dragging compatibility for IE, Opera, Firefox. <br/>
  91.         <font color="red">dragging everywhere</font>
  92.     </div>
  93. </div>
  94. </body>
  95. </html>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值