<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    efa's blog

    以用戶角度出發,你就已經成功一半了.

    導航

    <2005年6月>
    2930311234
    567891011
    12131415161718
    19202122232425
    262728293012
    3456789

    統計

    常用鏈接

    留言簿(18)

    我參與的團隊

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    Bi report

    dba

    info security

    other

    perl

    php

    python

    tech blogs

    tech websites

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    [WEB BASE] 獲得DIV容器中控件的真實位置

               今天在修正一些需求,在使用以往封裝好日期htc,發現了一個bug,如下,在正常情況下點擊輸入框可將隱藏的htc控件能顯示在input正下方。
               但在滾動層的時候再點擊輸入框就出現了問題了。
               由于blogjava不支持htc上傳。所以沒演示 : (

    有BUG的日歷HTC
    日期:

              測試可發現日歷控件偏位了。滾動條向下劃,它也不斷向下偏離input。

    查看htc源碼。發現其是用getDim(el)函數取得input的X,Y 坐標然后將日歷控制定位的。
     
    有bug的取得控件位置的javascript代碼
    function getDim(el){
     for (var lx=0,ly=0;el!=null;
    lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent); // 用offsetLeft , offsetTop 循環累加
     return {x:lx,y:ly}  //返回 input模式x,y 坐標
    }


    現在將input 框架放到div 容器中,然而也設置了CSS(position:relative;overflow:auto; )
     overflow:auto是指當 內容超出塊的情況下,自動顯示滾動條
    如圖分析:
    box.gif
    圖1是正常沒有滾動的情況,
    圖2、3是滾動后的效果(這兩種情況就會出現問題)

    原有bug代碼
     for (var lx=0,ly=0;el!=null;
    lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
    用offsetParent 中的offsetLeft  ,offsetTop 不斷累加, 在通常情況下是沒有問題的。
    但放到容器中,并且有滾動條的情況就會出現問題了。

    先來回顧一下dhtml中對象中的幾個屬性(更多請看DHTL參考書)

    offsetParent 
    ------------------------
    Retrieves a reference to the container object that defines the offsetTop and offsetLeft properties of the object.

    offsetHeight 
    ------------------------
    Retrieves the height of the object relative to the layout or coordinate parent, 
    as specified by the offsetParent property.  

    offsetLeft 
    ------------------------
    Retrieves the calculated left position of the object relative to the layout or coordinate parent, 
    as specified by the offsetParent property.  

    offsetParent 
    ------------------------
    Retrieves a reference to the container object that defines the offsetTop and offsetLeft properties of the object. 

    offsetTop  
    ------------------------
    Retrieves the calculated top position of the object relative to the layout or coordinate parent, as specified by the offsetParent property.  

    offsetWidth 
    ------------------------
    Retrieves the width of the object relative to the layout or coordinate parent, 
    as specified by the offsetParent property.  


    clientHeight 
    ------------------------
    Retrieves the height of the object including padding, but not including margin, border, or scroll bar.  

    clientLeft 
    ------------------------
    Retrieves the distance between the offsetLeft property and the true left side of the client area. 
     
    clientTop 
    ------------------------
    Retrieves the distance between the offsetTop property and the true top of the client area.  

    clientWidth 
    ------------------------
    Retrieves the width of the object including padding, but not including margin, border, or scroll bar. 


    scrollHeight 
    ------------------------
    Retrieves the scrolling height of the object.  

    scrollLeft 
    ------------------------
    Sets or retrieves the distance between the left edge of the object and the leftmost portion of the content currently visible in the window.  

    scrollTop 
    ------------------------
    Sets or retrieves the distance between the top of the object and the topmost portion of the content currently visible in the window.  

    scrollWidth 
    ------------------------
    Retrieves the scrolling width of the object. 

    顯然,原有bug的代碼使用 el.offsetTop  (取得了當前object相對于其父object的距離),
    當div滾動時。
    這樣計算其真實高度就會有問題(大于其實際的高度)


    代碼改進,當發現父的tag name 為DIV容器,并設置了相關的style   CSS(position:relative;overflow:auto; )
    那么我們就不再累加這個 offsetTop,取而代之的公式應該是再減去滾動上去的隱藏了的高度,也就是的值,如上圖可清晰發現:

     ly = ly + (el.offsetTop - el.scrollTop);


    修正后取得控件位置的javascript代碼,另外左右滾動的相應原理,在這里沒寫出來

    function getDim(el){
     for (var lx=0,ly=0;el!=null;
      lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent)
      {
          if(el.tagName.toLowerCase()=="div" && el.style.position=="relative")
          {
              ly = ly - el.offsetTop;  
              ly = ly + (el.offsetTop - el.scrollTop);
          }
      };
     return {x:lx,y:ly}
    }


    BUG 修正后日歷HTC
    日期:


    補充:
    Box model

    posted on 2005-06-29 21:01 一凡@ITO 閱讀(2227) 評論(4)  編輯  收藏

    評論

    # re: [WEB BASE] 獲得DIV容器中控件的真實位置 2005-06-30 09:48 emu

    只考慮了父容器是div的情況,有沒有考慮過有寫時候我們會有div嵌套之類的情況呢?
    以前看很多人計算的時候都是一層層的遞歸拿上一層的位置累加來確定當前元素在頁面中的真實位置。
    另一種可能的做法是,讓要插入的元素和原來的元素放在同一個容器中,再確定他們的相對位置。  回復  更多評論   

    # re: [WEB BASE] 獲得DIV容器中控件的真實位置 2005-06-30 10:53 一天一點

    div嵌套之類的情況是沒有問題。
    我這里用的是for循環所有父

    for (var lx=0,ly=0;el!=null;
    lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent)


    每出現,再處理
    if(el.tagName.toLowerCase()=="div" && el.style.position=="relative")
    {
    ly = ly - el.offsetTop;
    ly = ly + (el.offsetTop - el.scrollTop);
    }   回復  更多評論   

    # re: [WEB BASE] 獲得DIV容器中控件的真實位置 2005-06-30 15:16 emu

    呵呵原來遞歸在這里啊:
    el=el.offsetParent
    早上沒仔細看。  回復  更多評論   

    # re: [WEB BASE] 獲得DIV容器中控件的真實位置 2007-08-10 14:26 陳高昌

    你的這個問題沒有完全解決,當你手動去移動滾動條的時候,scrollTop會受影響,導致算出來的結果不一樣  回復  更多評論   


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲视频一区在线播放| 久久精品国产亚洲av成人| 亚洲中文无码线在线观看| 野花香高清视频在线观看免费| 亚洲阿v天堂在线2017免费| 羞羞视频在线免费观看| 亚洲成?v人片天堂网无码| 色婷婷综合缴情综免费观看| 国产精品亚洲美女久久久| 岛国精品一区免费视频在线观看| 4338×亚洲全国最大色成网站| xxxxx做受大片在线观看免费| 久久精品国产亚洲Aⅴ蜜臀色欲 | 亚洲AV无码专区日韩| 国产精品免费久久久久久久久| 免费a级毛片无码av| 美女网站在线观看视频免费的| 亚洲精品无码鲁网中文电影| 人人玩人人添人人澡免费| 亚洲成熟xxxxx电影| 我的小后妈韩剧在线看免费高清版 | 37pao成人国产永久免费视频 | 国产l精品国产亚洲区在线观看| a级片免费在线播放| xxxxx做受大片在线观看免费| 中文字幕亚洲专区| 一级毛片免费观看| 亚洲美国产亚洲AV| 久久亚洲色一区二区三区| 美女内射无套日韩免费播放| 国产精品亚洲片夜色在线| 亚洲成片观看四虎永久| 大地资源网高清在线观看免费 | 无码少妇一区二区浪潮免费| 亚洲AV成人一区二区三区观看| 亚洲人成网站18禁止一区| 4455永久在线观免费看| 美女被免费视频网站a| 久久精品国产亚洲AV麻豆网站| 全免费a级毛片免费看无码| 中文字幕视频在线免费观看|