镀铬上的偏移顶部问题

时间:2014-04-22 10:23:35

标签: jquery

我在jQuery中遇到了òffset().top属性的问题。它适用于除Chrome之外的所有浏览器。

我在桌子上工作,所以offset().top将在单元格上...

我的代码:

var topOfCell = $('td').offset().top;
var bottomOfCell= $('td').offset().top + $('td').outerHeight();

Chrome上的结果:

enter image description here

关于Firefox的结果,即等等......

enter image description here

1 个答案:

答案 0 :(得分:1)

offset()方法获取相对于文档的位置,并且可能会改变浏览器的浏览器,因此使用position()方法可以解决相对于父偏移获取位置的问题。为此,您应该在父元素中应用css位置。

因此,请尝试使用position()方法

或者,可能是偏移顶部是在加载内容之前获取值,因此请尝试在准备好时使用它:

$(document).ready(function(){
//do stuff here
});

或者,尝试在窗口加载功能上使用它:

window.onload = function(){
//do your stuff here
}