allen
          專注于java ee技術,包括struts,jsf,webwork,spring,hibernate,ibatis
          posts - 7,  comments - 9,  trackbacks - 0
          獲取距窗口的y值
          function getTop(e){
              var offset=e.offsetTop;
              if(e.offsetParent!=null) offset+=getTop(e.offsetParent);
              return offset;
          }
          獲取距窗口的x值
          function getLeft(e){
              var offset=e.offsetLeft;
              if(e.offsetParent!=null) offset+=getLeft(e.offsetParen)
              return offset;
          }

          除了上面的函數以外,還有一種快速方法,可以立刻獲得網頁元素的位置。

          那就是使用getBoundingClientRect()方法。它返回一個對象,其中包含了left、right、top、bottom四個屬性,分別對應了該元素的左上角和右下角相對于瀏覽器窗口(viewport)左上角的距離。

          所以,網頁元素的相對位置就是

           

           var X= this.getBoundingClientRect().left;

           
          var Y =this.getBoundingClientRect().top;

           

          再加上滾動距離,就可以得到絕對位置

           

           var X= this.getBoundingClientRect().left+document.documentElement.scrollLeft;

           
          var Y =this.getBoundingClientRect().top+document.documentElement.scrollTop;
          posted on 2011-11-28 14:43 robbin163 閱讀(213) 評論(0)  編輯  收藏

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


          網站導航:
           

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 奇台县| 北川| 池州市| 隆昌县| 枞阳县| 塔城市| 明溪县| 彭山县| 武川县| 梅州市| 扶风县| 磴口县| 惠安县| 鄂州市| 德昌县| 武夷山市| 内江市| 广德县| 芜湖县| 海林市| 和林格尔县| 健康| 游戏| 宣恩县| 政和县| 和龙市| 河津市| 南部县| 桦川县| 营山县| 油尖旺区| 九寨沟县| 富蕴县| 松溪县| 宜春市| 兴业县| 赤水市| 临桂县| 镇安县| 三河市| 灌南县|