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

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

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

    efa's blog

    以用戶角度出發(fā),你就已經(jīng)成功一半了.

    導(dǎo)航

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

    統(tǒng)計(jì)

    常用鏈接

    留言簿(18)

    我參與的團(tuán)隊(duì)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    Bi report

    dba

    info security

    other

    perl

    php

    python

    tech blogs

    tech websites

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

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

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

    有BUG的日歷HTC
    日期:

              測試可發(fā)現(xiàn)日歷控件偏位了。滾動(dòng)條向下劃,它也不斷向下偏離input。

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


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

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

    先來回顧一下dhtml中對象中的幾個(gè)屬性(更多請看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  (取得了當(dāng)前object相對于其父object的距離),
    當(dāng)div滾動(dòng)時(shí)。
    這樣計(jì)算其真實(shí)高度就會(huì)有問題(大于其實(shí)際的高度)


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

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


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

    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
    日期:


    補(bǔ)充:
    Box model

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

    評論

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

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

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

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

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


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

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

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

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

    你的這個(gè)問題沒有完全解決,當(dāng)你手動(dòng)去移動(dòng)滾動(dòng)條的時(shí)候,scrollTop會(huì)受影響,導(dǎo)致算出來的結(jié)果不一樣  回復(fù)  更多評論   


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲伦理一二三四| 日本高清不卡中文字幕免费| 成人免费777777| www永久免费视频| 亚洲精品成人久久| 免费国产成人高清视频网站| 国产成人精品无码免费看 | 亚洲人成电影福利在线播放| 青春禁区视频在线观看直播免费| 一级人做人爰a全过程免费视频| 亚洲欧洲国产精品你懂的| 免费在线观看亚洲| 最近最新高清免费中文字幕| 国产偷国产偷亚洲高清人| 亚洲精品中文字幕无码AV| 亚洲av午夜精品一区二区三区| 麻豆高清免费国产一区| 成年免费大片黄在线观看com| 亚洲白色白色在线播放| 亚洲综合另类小说色区色噜噜| 亚洲国产精品免费观看| 三级黄色免费观看| 国产产在线精品亚洲AAVV| 亚洲剧情在线观看| 亚洲色中文字幕无码AV| 日韩精品视频免费网址| 亚洲免费观看在线视频| 天黑黑影院在线观看视频高清免费| 亚洲成a∨人片在无码2023| 亚洲视频中文字幕| 亚洲乱码中文字幕手机在线| 免费国产黄线在线观看| 国产啪精品视频网站免费尤物 | 老外毛片免费视频播放| 亚洲AV无码一区二区三区人| 99在线视频免费观看视频| 日本高清高色视频免费| 免费的黄色网页在线免费观看| 亚洲午夜成人精品无码色欲| 亚洲国产精品无码久久久| 亚洲视频在线一区二区三区|