最佳答案
我已经读到,offsetLeft
和 offsetTop
不能在所有的浏览器正常工作。jQuery.offset()
应该为此提供一个抽象,以提供正确的值 x 浏览器。
我要做的是获取一个元素相对于该元素左上角被单击的位置的坐标。
问题是 jQuery.offset().top
实际上在 FFX 3.6中给了我一个十进制值(在 IE 和 Chrome 中,这两个值是匹配的)。
这个小提琴 展示了这个问题。如果你点击底部图像,jQuery.offset().top
返回327.5,但是 offsetTop
返回328。
我认为 offset()
正在返回正确的值,我应该使用它,因为它可以跨浏览器工作。然而,人们显然不能点击像素的小数。确定到 Math.round()
的真偏移量的正确方法是否是 jQuery 返回的偏移量?我应该使用 offsetTop
代替,还是完全使用其他方法?