offsetTop 与 jQuery.offset().top
我读到 offsetLeft
和 offsetTop
无法在所有浏览器中正常工作。 jQuery.offset() 应该为此提供一个抽象,以提供正确的值 xbrowser。
我想要做的是获取元素被单击的位置相对于元素左上角的坐标。
问题是 jQuery.offset().top 实际上在 FFX 3.6 中给了我一个十进制值(在 IE 和 Chrome 中,这两个值匹配)。
这个小提琴展示了这个问题。如果您单击底部图像,jQuery.offset().top
返回 327.5,但 offsetTop
返回 328。
我想 offset()
返回code> 返回正确的值,我应该使用它,因为它可以跨浏览器工作。然而,人们显然无法点击像素的小数点。确定 Math.round()
的真实偏移量的正确方法是 jQuery 返回的偏移量吗?我应该使用 offsetTop
来代替,还是完全使用其他方法?
I have read that offsetLeft
and offsetTop
do not work properly in all browsers. jQuery.offset()
is supposed to provide an abstraction for this to provide the correct value xbrowser.
What I am trying to do is get the coordinates of where an element was clicked relative to the top-left of the element.
Problem is that jQuery.offset().top
is actually giving me a decimal value in FFX 3.6 (in IE and Chrome, the two values match).
This fiddle exhibits the issue. If you click the bottom image, jQuery.offset().top
returns 327.5, but offsetTop
returns 328.
I would like to think that offset()
is returning the correct value and I should use it because it will work across browsers. However, people obviously cannot click decimals of pixels. Is the proper way to determine the true offset to Math.round()
the offset that jQuery is returning? Should I use offsetTop
instead, or some other method entirely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是 jQuery API Doc 关于
.offset()
的内容:这是 MDN Web API 关于
的说法.offsetTop
:这是 jQuery v.1.11
.offset()
在获取坐标时基本上执行的操作:pageYOffset
直观地说明了多少滚动的页面docElem.scrollTop
是 IE<9 的后备(顺便说一句,jQuery 2 不支持)docElem.clientTop
是元素(本例中为文档)上边框的宽度elem.getBoundingClientRect()
获取相对于documentviewport 的坐标(见评论)。它可能会返回小数值,因此这是错误的根源。当页面缩放时,它还可能会导致 IE<8 出现错误。为了避免出现分数,请尝试迭代计算位置结论
element.offsetTop
。如果您想考虑父级滚动,请添加element.scrollTop
。 (或者使用 jQuery .position() 如果您是该库的粉丝)element.getBoundingClientRect().top
。如果您想考虑文档滚动,请添加window.pageYOffset
。如果文档没有边框(通常没有),则不需要减去文档的clientTop
,因此您可以相对于 document元素.clientTop
如果您不将元素边框视为元素的一部分This is what jQuery API Doc says about
.offset()
:This is what MDN Web API says about
.offsetTop
:This is what jQuery v.1.11
.offset()
basically do when getting the coords:pageYOffset
intuitively says how much was the page scrolleddocElem.scrollTop
is the fallback for IE<9 (which are BTW unsupported in jQuery 2)docElem.clientTop
is the width of the top border of an element (the document in this case)elem.getBoundingClientRect()
gets the coords relative to thedocumentviewport (see comments). It may return fraction values, so this is the source of your bug. It also may cause a bug in IE<8 when the page is zoomed. To avoid fraction values, try to calculate the position iterativelyConclusion
element.offsetTop
. Addelement.scrollTop
if you want to take the parent scrolling into account. (or use jQuery .position() if you are fan of that library)element.getBoundingClientRect().top
. Addwindow.pageYOffset
if you want to take the document scrolling into account. You don't need to subtract document'sclientTop
if the document has no border (usually it doesn't), so you have position relative to the documentelement.clientTop
if you don't consider the element border as the part of the element我认为你说人们不能点击半像素是对的,所以就我个人而言,我会使用圆角 jQuery 偏移......
I think you are right by saying that people cannot click half pixels, so personally, I would use rounded jQuery offset...
试试这个:
parseInt(jQuery.offset().top, 10)
Try this:
parseInt(jQuery.offset().top, 10)
offset
可能是非整数,使用em
作为测量单位,相对font-sizes
in%
。我还推测,当缩放不是 100% 时,偏移量可能不是整数,但这取决于浏览器如何处理缩放。
It is possible that the
offset
could be a non-integer, usingem
as the measurement unit, relativefont-sizes
in%
.I also theorise that the
offset
might not be a whole number when thezoom
isn't100%
but that depends how the browser handles scaling.您可以使用
parseInt(jQuery.offset().top)
在所有浏览器中始终使用 Integer(原始 -int
)值。You can use
parseInt(jQuery.offset().top)
to always use the Integer (primitive -int
) value across all browsers.