在Web开发中如果要想随心所欲的自由控制HTML元素,我们很多时候需要获得HTML元素在页面中的绝对位置(就是相对于浏览器的左上角坐标(0,0))。
这个位置的获取其实并不难,由于每个Html元素提供了一组和位置相关的属性,他们是: offsetLeft、offsetTop和offsetParent,还有两个带offset叫offsetWidth和offsetHeight,不过这俩和我们要说的取Html元素绝对位置没有太大关系。
属性offsetLeft和offsetTop就是Html元素相对于自己的offsetParent元素的位置,所以我们一般使用如下代码获得指定元素的绝对位置:
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
这个代码及其类似代码其实是比较常见,可是使用它来计算复杂的页面布局却会有问题,有什么问题呢?这需要了解了Web页中的HTML元素的排版布局规则后,才能很好的理解。
上次在获取Html元素在页面中的绝对位置及问题(1)中说了获取页面元素绝对位置的方法,最后又说到那个方法是有问题的。为了理解这个问题,希望先看一下这两篇文章再看下面文(Web页中的HTML元素的排版布局规则、用户定制Web页中元素的排版布局策略)。
第二篇文章最后比较清楚的说明了,当我们的元素所处的viewport不是<body>的时候,其定位是从自己的viewport的(0,0)开始计算的。这个时候我们如果用获取...(1)中的办法去取绝对位置,将产生错误的定位效果@_@。所以为了避免出错我们需要找到被计算绝对位置的元素的viewport,然后把它的offsetTop和offsetLeft累计到其viewport的(0,0)处为止。
当我们需要计算的元素的offsetPerent满足这个条件:elmt.style.position == 'absolute' || elmt.style.position == 'relative' || ( elmt.style.overflow != 'visible' && elmt.style.overflow != '' ),它将是一个viewport,也就是说会影响绝对定位的计算,我们应该在此停止offset的累加。
更新过的方法叫GetAbsoluteLocationEx,代码附后:
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
![](http://snailwong.javaeye.com/Images/OutliningIndicators/None.gif)
如果offsetParent没有设置position和overflow属性,GetAbsoluteLocation和GetAbsoluteLocationEx的计算结果完全相同的,也就是说GetAbsoluteLocationEx向下兼容GetAbsoluteLocation。
转自:http://blog.163.com/da7_1@126/blog/static/10407267820107745226109/
我现在用的是getBoundingClientRect()方法,这个方法以前是ie独有的 ,但是现在慢慢其他浏览器也都支持了,我测试的firefox和chrome,opera最新版都没有问题,网上说的是FF3+,opera9.5+,safari 4,就已经支持这个方法var X= this.getBoundingClientRect().left+document.documentElement.scrollLeft; var Y =this.getBoundingClientRect().top+document.documentElement.scrollTop;简单的两句话就搞定了,这些大家可能在网上都看见过了,这里我在说一点自己使用的时候发现的小问题,
就是getBoundingClientRect()方法返回的对象中,height和width属性在ie9之前是不支持的,就是说ie9之前的getBoundingClientRect()方法返回的对象中只有四个属性:top,left,bottom,right,四个,ie9新增:height和width,只能是bottom-top来手动计算,不能直接调用,
其他的浏览器最新版(我电脑上就装了最新版,不好意思)是没有这个问题的,