完全兼容ie与firefox的拖动层

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html;charset=GBK">
  5. <title>zhou's html</title>
  6. </head>
  7. <body>
  8.                                                 一共三个层,下面还有一个,把他拖上来
  9. <div id="test" style='cursor:move;border:6px solid #000000;left:1px;top:2px;width:100px;height:100px;background-color:#F9F7ED;position:absolute;text-align:center'><span>Magnolia Mag.nolia</span><input type="text" style="width:50px" maxlength="5"/></div>
  10. <div id="test1" style='cursor:move;border:6px solid #000000;left:1px;top:200px;width:150px;height:150px;background-color:#FF1A00;position:absolute;text-align:center'><span>Mozilla Red</span><br/><input type="text" style="width:80px" maxlength="9"/></div>
  11. <div id="test3" style="border:3px solid #C3D9FF;text-align:center;left:700px;top:100px;width:150px;height:250px;background-color:#ffffff;position:absolute"><div id="test31" style="cursor:move;left:300px;top:200px;width:150px;height:30px;background-color:#C3D9FF;text-align:center;vertical-align:center;line-height:30px;font-weight:bold;color:#ffffff">小 纸 条</div>一共三个层,下面还有一个,把他拖上来<br/><textarea style="width:130px;height:150px;">有了拖动层,做局部拖动很简单</textarea><br/><input type="button" value="submit it"/></div>
  12. <div id="test4" style="border:3px solid #CDEB8B;text-align:center;left:480px;top:150px;width:150px;height:250px;background-color:#ffffff;position:absolute"><div id="test41" style="cursor:move;left:300px;top:200px;width:150px;height:30px;background-color:#CDEB8B;text-align:center;vertical-align:center;line-height:30px;font-weight:bold;color:#ffffff">小 纸 条</div>一共三个层,下面还有一个,把他拖上来<br/><textarea style="width:130px;height:150px;">顺便练习下颜色搭配</textarea><br/><input type="button" value="submit it"/></div>
  13. <div id="test2" style='cursor:move;border:6px solid #000000;left:200px;top:800px;width:100px;height:100px;background-color:#EEEEEE;position:absolute;text-align:center'><span>Shiny silver</span><br/><input type="button" value="hit me"/></div>
  14. <br/><br/><br/><br/><br/><br/><br/><br/><br/>
  15. <script type="text/javascript" charset="utf-8">
  16. var $=function(id){return document.getElementById(id)};
  17. Array.prototype.extend=function(C){for(var B=0,A=C.length;B<A;B++){this.push(C[B]);}return this;}
  18. function divDrag(){
  19.         var A,B,$cn;
  20.         var zIndex=1;
  21.         this.dragStart=function(e){
  22.                 ee=e||window.event;
  23.                 if((e.which&&(e.which!=1))||(e.button&&(e.button!=1)))return;
  24.                 var pos=this.$pos;
  25.                 $cn=this.parent||this;
  26.                 if(document.defaultView){
  27.                     _top=document.defaultView.getComputedStyle($cn,null).getPropertyValue("top");
  28.                     _left=document.defaultView.getComputedStyle($cn,null).getPropertyValue("left");}
  29.                 else{
  30.                     if($cn.currentStyle){_top=$cn.currentStyle["top"];_left=$cn.currentStyle["left"];}}
  31.                 pos.ox=(e.pageX||(e.clientX+document.documentElement.scrollLeft))-parseInt(_left);
  32.                 pos.oy=(e.pageY||(e.clientY+document.documentElement.scrollTop))-parseInt(_top);
  33.                 if(!!A){
  34.                      if(document.removeEventListener){
  35.                         document.removeEventListener("mousemove",A,false);
  36.                         document.removeEventListener("mouseup",B,false);}
  37.                     else{
  38.                         document.detachEvent("onmousemove",A);
  39.                         document.detachEvent("onmouseup",B);}}
  40.                 A=this.dragMove.create(this);
  41.                 B=this.dragEnd.create(this);
  42.                 if(document.addEventListener){
  43.                     document.addEventListener("mousemove",A,false);
  44.                     document.addEventListener("mouseup",B,false);}
  45.                 else{
  46.                     document.attachEvent("onmousemove",A);
  47.                     document.attachEvent("onmouseup",B);}
  48.                 $cn.style.zIndex=(++zIndex);
  49.                 this.stop(e);
  50.         }
  51.         this.dragMove=function(e){
  52.                 ee=e||window.event;
  53.                 var pos=this.$pos;
  54.                 $cn=this.parent||this;
  55.                 $cn.style.top=(e.pageY||(e.clientY+document.documentElement.scrollTop))-parseInt(pos.oy)+'px';
  56.                 $cn.style.left=(e.pageX||(e.clientX+document.documentElement.scrollLeft))-parseInt(pos.ox)+'px';
  57.                 this.stop(e);}
  58.         this.dragEnd=function(e){
  59.                 var pos=this.$pos;              
  60.                 ee=e||window.event;
  61.                 if((e.which&&(e.which!=1))||(e.button&&(e.button!=1)))return;
  62.                 $cn=this.parent||this;
  63.                 if(!!(this.parent)){this.style.backgroundColor=pos.color}
  64.                 if(document.removeEventListener){
  65.                         document.removeEventListener("mousemove",A,false);
  66.                         document.removeEventListener("mouseup",B,false);}
  67.                 else{
  68.                         document.detachEvent("onmousemove",A);
  69.                         document.detachEvent("onmouseup",B);}
  70.                 A=null;
  71.                 B=null;
  72.                 $cn.style.zIndex=(++zIndex);
  73.                 this.stop(e);
  74.         }
  75.         this.shiftColor=function(){
  76.             this.style.backgroundColor="#EEEEEE";   
  77.         }
  78.         this.position=function (e){ 
  79.                 var t=e.offsetTop;
  80.                 var l=e.offsetLeft;
  81.                 while(ee=e.offsetParent){ 
  82.                                 t+=e.offsetTop; 
  83.                                 l+=e.offsetLeft;}
  84.                 return {x:l,y:t,ox:0,oy:0,color:null}
  85.         }
  86.         this.stop=function(e){
  87.                 if(e.stopPropagation){e.stopPropagation();}else{e.cancelBubble=true;}
  88.                 if(e.preventDefault){e.preventDefault();}else{e.returnValue=false;}
  89.         }
  90.         this.stop1=function(e){
  91.             ee=e||window.event;
  92.             if(e.stopPropagation){e.stopPropagation();}else{e.cancelBubble=true;}
  93.         }
  94.         this.create=function(bind){
  95.                 var B=this;
  96.                 var A=bind;
  97.                 return function(e){return B.apply(A,[e]);}
  98.         }
  99.         thisthis.dragStart.create=this.create;
  100.         thisthis.dragMove.create=this.create;
  101.         thisthis.dragEnd.create=this.create;
  102.         thisthis.shiftColor.create=this.create;
  103.         this.initialize=function(){
  104.                 for(var A=0,B=arguments.length;A<B;A++){
  105.                         C=arguments[A];
  106.                         if(!(C.push)){C=[C];}
  107.                         $C=(typeof(C[0])=='object')?C[0]:(typeof(C[0])=='string'?$(C[0]):null);
  108.                         if(!$C)continue;
  109.                         $C.$pos=this.position($C);
  110.                         $C.dragMove=this.dragMove;
  111.                         $C.dragEnd=this.dragEnd;
  112.                         $C.stop=this.stop;
  113.                         if(!!C[1]){$CC.parent=C[1];$C.$pos.color=$C.style.backgroundColor;}
  114.                         if($C.addEventListener){
  115.                             $C.addEventListener("mousedown",this.dragStart.create($C),false);
  116.                             if(!!C[1]){$C.addEventListener("mousedown",this.shiftColor.create($C),false);}}
  117.                         else{$C.attachEvent("onmousedown",this.dragStart.create($C));
  118.                             if(!!C[1]){$C.attachEvent("onmousedown",this.shiftColor.create($C));}}
  119.                 }
  120.         }
  121.         this.initialize.apply(this,arguments);
  122. }
  123. //生成拖动层很简单,只需要(参数之一如果是数组表示局部拖动,arr[0]表示拖动层,arr[1]表示整体)
  124. new divDrag(
  125.     ['test'],
  126.     [$('test31'),$('test3')],
  127.     $('test1')
  128.     ,$('test2')
  129.     ,[$('test41'),$('test4')]);
  130. </script>
  131. </body>
  132. </html>

****************************************************************************

下面这个是可以选取拖动层上的文字和输入框的实现,其实就是在上面基础上添加了几个事件

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  5. <title>zhou's html</title>
  6. </head>
  7. <body>
  8.                                                 一共三个层,下面还有一个,把他拖上来,按下文字和button是不可以拖动的//一共三个层,下面还有一个,把他拖上来,按下文字和button是不可以拖动的//一共三个层,下面还有一个,把他拖上来,按下文字和button是不可以拖动的
  9. <div id="test" style='border:6px solid #000000;left:1px;top:2px;width:100px;height:100px;background-color:#F9F7ED;position:absolute;text-align:center'><span>Magnolia Mag.nolia</span><input type="text" style="width:50px" maxlength="5"/></div>
  10. <div id="test1" style='border:6px solid #000000;left:1px;top:200px;width:150px;height:150px;background-color:#FF1A00;position:absolute;text-align:center'><span>Mozilla Red</span><br/><input type="text" style="width:80px" maxlength="9"/></div>
  11. <div id="test2" style='border:6px solid #000000;left:200px;top:800px;width:100px;height:100px;background-color:#EEEEEE;position:absolute;text-align:center'><span>Shiny silver</span><br/><input type="button" value="hit me"/></div>
  12. <br/><br/><br/><br/><br/><br/><br/><br/><br/>
  13. <script type="text/javascript" charset="utf-8">
  14. var $=function(id){return document.getElementById(id)};
  15. Array.prototype.extend=function(C){for(var B=0,A=C.length;B<A;B++){this.push(C[B]);}return this;}
  16. function divDrag(){
  17.                 var A,B;
  18.         var zIndex=1;
  19.         this.dragStart=function(e){
  20.                 ee=e||window.event;
  21.                 if((e.which&&(e.which!=1))||(e.button&&(e.button!=1)))return;
  22.                 var pos=this.$pos;
  23.                                 if(document.defaultView){
  24.                                         _top=document.defaultView.getComputedStyle(this,null).getPropertyValue("top");
  25.                                         _left=document.defaultView.getComputedStyle(this,null).getPropertyValue("left");}
  26.                                 else{
  27.                                         if(this.currentStyle){_top=this.currentStyle["top"];_left=this.currentStyle["left"];}
  28.                                 }
  29.                 pos.ox=(e.pageX||(e.clientX+document.documentElement.scrollLeft))-parseInt(_left);
  30.                 pos.oy=(e.pageY||(e.clientY+document.documentElement.scrollTop))-parseInt(_top);
  31.                                 if(!!A){
  32.                                          if(document.removeEventListener){
  33.                         document.removeEventListener("mousemove",A,false);
  34.                         document.removeEventListener("mouseup",B,false);
  35.                                         }else{
  36.                         document.detachEvent("onmousemove",A);
  37.                         document.detachEvent("onmouseup",B);
  38.                                                 document.detachEvent("ondragstart",G);
  39.                         }
  40.                                 }
  41.                                 A=this.dragMove.create(this);
  42.                 B=this.dragEnd.create(this);
  43.                 if(document.addEventListener){
  44.                                         document.addEventListener("mousemove",A,false);
  45.                     document.addEventListener("mouseup",B,false);
  46.                 }else{
  47.                                         document.attachEvent("onmousemove",A);
  48.                     document.attachEvent("onmouseup",B);
  49.                                                 G=function(){return false};
  50.                                                 document.attachEvent("ondragstart",G);
  51.                 }
  52.                 this.style.zIndex=(++zIndex);
  53.                 this.stop(e);
  54.         }
  55.         this.dragMove=function(e){
  56.                                 ee=e||window.event;
  57.                                 var pos=this.$pos;
  58.                 this.style.top=(e.pageY||(e.clientY+document.documentElement.scrollTop))-parseInt(pos.oy)+'px';
  59.                 this.style.left=(e.pageX||(e.clientX+document.documentElement.scrollLeft))-parseInt(pos.ox)+'px';
  60.                                 this.stop(e);}
  61.         this.dragEnd=function(e){
  62.                 var pos=this.$pos;              
  63.                 ee=e||window.event;
  64.                                 
  65.                                 if((e.which&&(e.which!=1))||(e.button&&(e.button!=1)))return;
  66.                 if(document.removeEventListener){
  67.                         document.removeEventListener("mousemove",A,false);
  68.                         document.removeEventListener("mouseup",B,false);
  69.                                 }else{
  70.                         document.detachEvent("onmousemove",A);
  71.                         document.detachEvent("onmouseup",B);
  72.                                                 document.detachEvent("ondragstart",G);
  73.                 }
  74.                                 A=null;
  75.                                 B=null;
  76.                 this.style.zIndex=(++zIndex);
  77.                 this.stop(e);
  78.         }
  79.         this.position=function (e){ 
  80.                 var t=e.offsetTop;
  81.                 var l=e.offsetLeft;
  82.                 while(ee=e.offsetParent){ 
  83.                                 t+=e.offsetTop; 
  84.                                 l+=e.offsetLeft; 
  85.                 }
  86.                 return {x:l,y:t,ox:0,oy:0}
  87.         }
  88.         this.stop=function(e){
  89.                 if(e.stopPropagation){
  90.                         e.stopPropagation();
  91.                 }else{
  92.                         e.cancelBubble=true;}
  93.                         
  94.                 if(e.preventDefault){
  95.                         e.preventDefault();}
  96.                 else{e.returnValue=false;}
  97.         }
  98.                 this.stop1=function(e){
  99.                         ee=e||window.event;
  100.                 if(e.stopPropagation){
  101.                         e.stopPropagation();
  102.                 }else{
  103.                         e.cancelBubble=true;}
  104.         }
  105.         this.create=function(bind){
  106.                 var B=this;
  107.                 var A=bind;
  108.                 return function(e){
  109.                         return B.apply(A,[e]);
  110.                 }
  111.         }
  112.         thisthis.dragStart.create=this.create;
  113.         thisthis.dragMove.create=this.create;
  114.         thisthis.dragEnd.create=this.create;
  115.         
  116.         this.initialize=function(){
  117.                 for(var A=0,B=arguments.length;A<B;A++){
  118.                         C=arguments[A];
  119.                         C=(typeof(C)=='object')?C:(typeof(C)=='string'?$(C):null);
  120.                         if(!C)continue;
  121.                         C.$pos=this.position(C);
  122.                         C.dragMove=this.dragMove;
  123.                         C.dragEnd=this.dragEnd;
  124.                         C.position=this.position;
  125.                         C.stop=this.stop;
  126.                                                 var $A=[];
  127.                                                 $A=$A.extend(C.getElementsByTagName('span')||[]).extend(C.getElementsByTagName('input')||[]);
  128.                                                 for(var D=0,E=$A.length;D<E;D++){
  129.                                                         if(C.addEventListener){
  130.                                 $A[D].addEventListener("mousedown",this.stop1,false);
  131.                                                                 $A[D].addEventListener("mousemove",this.stop1,false);
  132.                                         }else{
  133.                                 $A[D].attachEvent("onmousedown",this.stop1);
  134.                                                                 $A[D].attachEvent("onmousemove",this.stop1);
  135.                                 }
  136.                                                 }
  137.                         if(C.addEventListener){
  138.                                 C.addEventListener("mousedown",this.dragStart.create(C),false);
  139.                         }else{
  140.                                 C.attachEvent("onmousedown",this.dragStart.create(C));
  141.                         }
  142.                 }
  143.         }
  144.         this.initialize.apply(this,arguments);
  145.         
  146. }
  147. //生成拖动层很简单
  148. new divDrag('test',$('test1'),$('test2'));
  149. </script>
  150. </body>
  151. </html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值