[转]JavaScript简单实现div拖拽

本文介绍了一种兼容IE、Opera和Firefox的元素拖曳方法,通过JavaScript实现,并提供了详细的代码示例。该方法允许用户在网页上拖动具有绝对定位的元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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 的坐标值, 如:
  1. linenum
  2.     
  3.         element 1
  4.         dragging compatibility for IE, Opera, Firefox. 
  5.     

函数 fDragging(obj, e, limit) 的各参数解释:
obj: HTML元素对象, 要拖曳的元素
e: 指定为 event 对象, 主要为兼容 Firefox
limit: 布尔值, 指定是否只能在父元素中拖曳, false 可移动至任何位置. 

函数 fDragging(obj, e, limit) 应该在 HTML onmousedown 属性 下使用, 如: 
  1. linenum
  2.     
  3.         element 
  4.         dragging compatibility for IE, Opera, Firefox. 
  5.     

shawl.qiu 
2006-11-10
http://blog.youkuaiyun.com/btbtd

函数: fDragging(obj, e, limit) 及使用演示
  1. linenum
  2. http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. http://www.w3.org/1999/xhtml">
  4. shawl.qiu template
  5. //
  6.     function fDragging(obj, e, limit){
  7.         if(!e) e=window.event;
  8.         var x=parseInt(obj.style.left);
  9.         var y=parseInt(obj.style.top);
  10.         
  11.         var x_=e.clientX-x;
  12.         var y_=e.clientY-y;
  13.         
  14.         if(document.addEventListener){
  15.             document.addEventListener('mousemove', inFmove, true);
  16.             document.addEventListener('mouseup', inFup, true);
  17.         } else if(document.attachEvent){
  18.             document.attachEvent('onmousemove', inFmove);
  19.             document.attachEvent('onmouseup', inFup);
  20.         }
  21.         
  22.         inFstop(e);    
  23.         inFabort(e)
  24.         
  25.         function inFmove(e){
  26.             var evt;
  27.             if(!e)e=window.event;
  28.             
  29.             if(limit){
  30.                 var op=obj.parentNode;
  31.                 var opX=parseInt(op.style.left);
  32.                 var opY=parseInt(op.style.top);
  33.                 
  34.                 if((e.clientX-x_)<0) return false;
  35.                 else if((e.clientX-x_+obj.offsetWidth+opX)>(opX+op.offsetWidth)) return false;
  36.                 
  37.                 if(e.clientY-y_<0) return false;
  38.                 else if((e.clientY-y_+obj.offsetHeight+opY)>(opY+op.offsetHeight)) return false;
  39.                 //status=e.clientY-y_;
  40.             }
  41.             
  42.             obj.style.left=e.clientX-x_+'px';
  43.             obj.style.top=e.clientY-y_+'px';
  44.             
  45.             inFstop(e);
  46.         } // shawl.qiu script
  47.         function inFup(e){
  48.             var evt;
  49.             if(!e)e=window.event;
  50.             
  51.             if(document.removeEventListener){
  52.                 document.removeEventListener('mousemove', inFmove, true);
  53.                 document.removeEventListener('mouseup', inFup, true);
  54.             } else if(document.detachEvent){
  55.                 document.detachEvent('onmousemove', inFmove);
  56.                 document.detachEvent('onmouseup', inFup);
  57.             }
  58.             
  59.             inFstop(e);
  60.         } // shawl.qiu script
  61.  
  62.         function inFstop(e){
  63.             if(e.stopPropagation) return e.stopPropagation();
  64.             else return e.cancelBubble=true;            
  65.         } // shawl.qiu script
  66.         function inFabort(e){
  67.             if(e.preventDefault) return e.preventDefault();
  68.             else return e.returnValue=false;
  69.         } // shawl.qiu script
  70.     }
  71. //]]>
  72.  this parent
  73.  
  74.     
  75.         element 
  76.         dragging compatibility for IE, Opera, Firefox. 
  77.     
  78.     
  79.         element 1
  80.         dragging compatibility for IE, Opera, Firefox. 
  81.     
  82.     
  83.         element 2
  84.         dragging compatibility for IE, Opera, Firefox. 
  85.          dragging everywhere
  86.     

 来源: http://blog.youkuaiyun.com/btbtd/archive/2006/11/10/1377810.aspx

 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值