Javascript 元素拖曳操作 By shawl.qiu (相容IE,Opera,Firefox)

來源:互聯網
上載者:User

Javascript  元素拖曳操作 By shawl.qiu (相容IE,Opera,Firefox)

說明: 
拖曳流程
滑鼠按下->(滑鼠移動->元素移動)
滑鼠按鍵彈起->元素停止移動

針對 IE, 主要使用 obj.attachEvent() && obj.detachEvent()
針對 Firefox 主要使用 DOM 2 的 obj.addEventListener() && obj.removeEventListener
Opera 以上兩種方法都支援

在本文中, 需要拖曳的元素必須指定style 屬性為 position:absolute; 
且應指定 left && top 的座標值, 如:

    linenum

  1.     <div style=" border:1px dashed blue; width: 180px; text-align:center; position:absolute; left:100px; top: 150px;" onmousedown="fDragging(this, event, true);">
  2.         element 1<br/>
  3.         dragging compatibility for IE, Opera, Firefox. 
  4.     </div>

函數 fDragging(obj, e, limit) 的各參數解釋:
obj: HTML元素對象, 要拖曳的元素
e: 指定為 event 對象, 主要為相容 Firefox
limit: 布爾值, 指定是否只能在父元素中拖曳, false 可移動至任何位置. 

函數 fDragging(obj, e, limit) 應該在 HTML onmousedown 屬性 下使用, 如: 

    linenum

  1.     <div style=" border:1px dashed blue; width: 180px; text-align:center; position:absolute; left:50px; top: 50px;" onmousedown="fDragging(this, event, true);">
  2.         element <br/>
  3.         dragging compatibility for IE, Opera, Firefox. 
  4.     </div>

shawl.qiu 
2006-11-10
http://blog.csdn.net/btbtd

函數: fDragging(obj, e, limit) 及使用示範

    linenum

  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;" onmousedown="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;" onmousedown="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;" onmousedown="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>


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.