shawl.qiu Javascript 元素拖曳类 Dragging class v1.0

shawl.qiu Javascript 元素拖曳类 Dragging class v1.0

说明:
其实早先也用 js写过一个相关函数, fDragging.
见:  http://blog.csdn.net/btbtd/archive/2006/11/10/1377810.aspx

不过说实在的, 真正应用起来还是有点麻烦的, 首先就是那个 css 的 position: absolute, 这个属性一般很难控制, 调用起来也够麻烦的.

因此嘛, 在鄙懒人一懒到底的精神支持下, 就写了这么个类出来了.
调用和设置, 那可不是一般的容易...
唉, 懒人没话说了, over.

目录:
1. 类 Dragging 及调用

shawl.qiu
2007-05-17
http://blog.csdn.net/btbtd

下载:
http://shawl.qiu.googlepages.com/shawl.qiu_js_class_Dragging_v1.0.htm.7z

演示:
http://shawl.qiu.googlepages.com/shawl.qiu_js_class_Dragging_v1.0.htm

内容:
1. 类 Dragging 及调用
  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.  /*-----------------------------------------------------------------------------------*/
  10.   * shawl.qiu javascript Dragging class v1.0
  11.  /*-----------------------------------------------------------------------------------*/
  12.  //---------------------------------begin class Dragging()-------------------------------//
  13.  function Dragging()
  14.  { // shawl.qiu code
  15.   //------------------------------------begin public variable
  16.   //---------------begin about
  17.   this.au = {}
  18.   this.au.Subject = "shawl.qiu javascript Dragging class";
  19.   this.au.Version = "v1.0";
  20.   this.au.Name = "shawl.qiu";
  21.   this.au.Email = "shawl.qiu@gmail.com";
  22.   this.au.Blog = "http://blog.csdn.net/btbtd";
  23.   this.au.CreatedDate = "2007-5-17";
  24.   this.au.Update = {};
  25.   this.au.Update["1"] = "";
  26.   //---------------end about
  27.   
  28.   this.Set = [];
  29.   this.AddElement = fAddElement;
  30.   
  31.   this.Count = 1;
  32.   //------------------------------------end public variable
  33.   
  34.   //------------------------------------begin private variable
  35.   var Tl = this;
  36.   //------------------------------------end private variable
  37.   
  38.   //------------------------------------begin public method
  39.   this.Go = fGo;
  40.   //------------------------------------end public method
  41.  
  42.   //------------------------------------begin private method
  43.   function fGo()
  44.   {
  45.    if(Tl.Set.length==0)
  46.    {
  47.     alert("对象集合为空!");
  48.     return false;
  49.    } // end if
  50.    
  51.    var Debug = true;
  52.    
  53.    for(var i=0, j=Tl.Set.length; i<j; i++)
  54.    {
  55.     var Obj = Tl.Set[i];
  56.     var Inherit = Obj["Inherit"];
  57.     with(Obj.style)
  58.     {
  59.      top = fFindPos(Obj, true)+"px";
  60.      left = fFindPos(Obj, false)+"px";
  61.     }
  62.    }
  63.    
  64.    for(var i=0, j=Tl.Set.length; i<j; i++)
  65.    {
  66.     var Obj = Tl.Set[i];
  67.     var Inherit = Obj["Inherit"];
  68.     with(Obj.style)
  69.     {
  70.      position = "absolute";
  71.     }
  72.     defaultStatus = 
  73.      " Obj.clientTop: "+Obj.offsetTop
  74.      +" Obj.clientLeft: "+Obj.offsetLeft
  75.      +" fFindPos(Obj, true): "+fFindPos(Obj, true)
  76.      +" fFindPos(Obj, false): "+fFindPos(Obj, false)
  77.     ;
  78.     
  79.     if(Debug)
  80.     {
  81.      defaultStatus =
  82.       "typeof(Obj): "+typeof(Obj)
  83.       +" Inherit: "+Inherit
  84.       ;
  85.     } // end if 
  86.     
  87.     Obj.onmousedown = 
  88.      function()
  89.      {
  90.       this.onmousemove = 
  91.        function(e)
  92.        {
  93.         if(!e) e = window.event;
  94.         var Parent = this.parentNode;
  95.         
  96.         if(this["Inherit"])
  97.         {
  98.          var Width = this.offsetWidth;
  99.          var Height = this.offsetHeight;
  100.          var eX = e.clientX;
  101.          var eY = e.clientY;
  102.          
  103.          var iPTop = fFindPos(Parent, true);
  104.          var iPLeft = fFindPos(Parent, false);
  105.          var iPRight = fFindPos(Parent, false)+Parent.offsetWidth;
  106.          var iPBottom = fFindPos(Parent, true)+Parent.offsetHeight; 
  107.          
  108.          var iRight = this.offsetLeft+this.offsetWidth;
  109.          var iBottom = this.offsetTop+this.offsetHeight;
  110.          
  111.          // check top
  112.          if(this.offsetTop<iPTop)
  113.          {
  114.           this.style.top = iPTop+"px";
  115.           return;
  116.          }
  117.          
  118.          // check left
  119.          if(this.offsetLeft<iPLeft)
  120.          {
  121.           this.style.left = iPLeft+"px";
  122.           return;
  123.          }
  124.          
  125.          // check right
  126.          if(iRight>iPRight)
  127.          {
  128.           this.style.left = iPRight-this.offsetWidth+"px";
  129.           return;
  130.          }
  131.          
  132.          // check bottom 
  133.          if(iBottom>iPBottom)
  134.          {
  135.           this.style.top = iPBottom-this.offsetHeight+"px";
  136.           return;
  137.          }
  138.          
  139.          with(this.style)
  140.          {
  141.           top = eY-(Height/2) +"px";
  142.           left = eX-(Width/2) +"px";
  143.           zIndex = 9999;
  144.          } // end with
  145.          
  146.          defaultStatus =
  147.          " this.offsetLeft: "+this.offsetLeft
  148.          +" this.offsetTop: "+this.offsetTop
  149.          +" iPTop: "+iPTop
  150.          +" iPLeft: "+iPLeft
  151.          +" iPRight: "+iPRight
  152.          +" iPBottom: "+iPBottom
  153.          +" iRight: "+iRight
  154.          +" iBottom: "+iBottom
  155.          ;
  156.          
  157.         }
  158.         else
  159.         {
  160.          var Width = this.offsetWidth;
  161.          var Height = this.offsetHeight;
  162.          var eX = e.clientX;
  163.          var eY = e.clientY;
  164.          
  165.          with(this.style)
  166.          {
  167.           position = "absolute";
  168.           top = eY-(Height/2) +"px";
  169.           left = eX-(Width/2) +"px";
  170.          } // end with
  171.          
  172.          defaultStatus =
  173.          " this.offsetLeft: "+this.offsetLeft
  174.          +" this.offsetTop: "+this.offsetTop
  175.          +" eX: "+eX
  176.          +" eY: "+eY
  177.          ;
  178.         }
  179.        } // end this.onmousemove
  180.        
  181.      } // end Obj.onmousedown
  182.      
  183.     Obj.onmouseup =
  184.      function()
  185.      {
  186.       this.style.zIndex = 5555;
  187.       ClearMouseMove(this);
  188.      } // end Obj.onmouseup
  189.      
  190.    } // end for
  191.    
  192.   } // end function fGo
  193.   
  194.   function ClearMouseMove(Obj)
  195.   {
  196.    if(Obj!=null)
  197.    {
  198.     Obj.onmousemove = null;
  199.    }
  200.   }
  201.   
  202.   function fAddElement(sId, bInherit)
  203.   {
  204.    var Obj;
  205.    if(typeof(sId)=="string")
  206.    {
  207.     Obj = document.getElementById(sId);
  208.     if(Obj==null)
  209.     {
  210.      alert("无法获得 id 为 : "+sId+" 的元素!");
  211.      return;
  212.     } // end if 1
  213.    } 
  214.    else if(typeof(sId)=="object")
  215.    {
  216.     Obj = sId;
  217.    }
  218.    else
  219.    {
  220.     alert("无法识别要获取的元素!");
  221.     return false;
  222.    }
  223.    
  224.    if(!bInherit) bInherit = false;
  225.    
  226.    var iIndex = Tl.Set.length;
  227.    Obj["Inherit"] = bInherit;
  228.    Tl.Set[iIndex] = Obj;
  229.    
  230.   } // end function fAddElement
  231.   
  232.   function fFindPos(oEle, bReturnY)
  233.   { // shawl.qiu script
  234.    if(!oEle)
  235.    {
  236.     alert("对象不能为空!");
  237.     return;
  238.    }
  239.    
  240.    if(bReturnY)
  241.    {
  242.     return fFindPosY(oEle);
  243.    }
  244.    else
  245.    {
  246.     return fFindPosX(oEle);
  247.    }
  248.    
  249.    function fFindPosX(oEle)
  250.    { // shawl.qiu script
  251.     var iLeft = 0;
  252.     if(oEle.offsetParent)
  253.     {
  254.      while(true)
  255.      {
  256.       iLeft += oEle.offsetLeft;
  257.       if(!oEle.offsetParent)
  258.       {
  259.         break;
  260.       } // end if 1
  261.       oEle = oEle.offsetParent;
  262.      } // end while
  263.     }
  264.     else if(oEle.x)
  265.     {
  266.       iLeft += oEle.x;
  267.     }
  268.     return iLeft;
  269.    } // end function fFindPosX
  270.    
  271.    function fFindPosY(oEle)
  272.    { // shawl.qiu script
  273.     var iTop = 0;
  274.     if(oEle.offsetParent)
  275.     {
  276.      while(true)
  277.      {
  278.       iTop += oEle.offsetTop;
  279.       if(!oEle.offsetParent)
  280.       {
  281.         break;
  282.       } // end if 1
  283.       oEle = oEle.offsetParent;
  284.      } // end while
  285.     }
  286.     else if(oEle.y)
  287.     {
  288.       iTop += oEle.y;
  289.     }
  290.     return iTop;
  291.    } // end function fFindPosX
  292.   } // end function fFindPos
  293.     
  294.   function fCkBrs()
  295.   {// shawl.qiu script
  296.    switch (navigator.appName)
  297.    {
  298.     case 'Opera': return 2;
  299.     case 'Netscape': return 3;
  300.     defaultreturn 1;
  301.    }
  302.   } // end function fCkBrs 
  303.   //------------------------------------end private method
  304.  } // shawl.qiu code
  305.  //---------------------------------end class Dragging()---------------------------------//
  306.  
  307.  onload = 
  308.   function()
  309.   {
  310.    var dragging = new Dragging();
  311.     // function fAddElement(sId, bInherit)
  312.     dragging.AddElement("m1", true);
  313.     dragging.AddElement("m2", false);
  314.     
  315.     dragging.Go();
  316.     
  317.     document.getElementById("subject").innerHTML = dragging.au.Subject+" "+dragging.au.Version;
  318.     
  319.     dragging = null;
  320.   }
  321. //]]>
  322. </script>
  323. </head>
  324. <body>
  325. <div style="width:80%; height:500px; border:1px dotted blue; background-color:white; margin:2% auto; " >
  326.  <div id="subject" style="text-align:center; font-weight:bold; padding:5px;">&nbsp;</div>

  327.  <div id="m1" style="width:200px; height:80px; border:1px dotted mistyrose; padding:2px 5px;">
  328.   inherit .............
  329.  </div>
  330.  &nbsp; 
  331.  <div id="m2" style="width:200px; height:80px; border:1px dotted mistyrose;padding:2px 5px;">
  332.   no inherit .............
  333.  </div>
  334.  
  335. </div>
  336. <br />
  337. <br />
  338. <br />
  339. <br />
  340. <br />
  341. <br />
  342. <br />
  343. <br />
  344. <br />
  345. <br />
  346. <br />
  347. <br />
  348. <br />
  349. <br />
  350. <br />
  351. <br />
  352. <br />
  353. <br />
  354. <br />
  355. <br />
  356. <br />
  357. <br />
  358. <br />
  359. <br />
  360. <br />
  361. <br />
  362. <br />
  363. <br />
  364. <br />
  365. <br />
  366. <br />
  367. <br />
  368. <br />
  369. <br />
  370. <br />
  371. <br />

  372. </body>
  373. </html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值