问题是,在某些页面上,偏移量可以完美地计算链接的位置,而在另一些页面上,偏移量大约是50个像素.我不知道为什么.$(".tiptrigger").mouseenter(function() { var s_id = $(this).attr('id'); var calc = $(this).offset().top; $("#tip-"+s_id).css({"margin-top": calc+"px"});});解决方案尝试将fixed用作弹出元素,而不是absolute.如果没有对body 进行不必要的填充,则比offset().top更有效如果仍然不准确,而不是.offset().top,请给 https://developer.mozilla.org/zh-CN/docs/Web/API/element.getBoundingClientRect I am using jQuery offset().top to calculate a hyperlink's pixels from the top of the document window so that when hovered, a tooltip can appear above it. By default, the tooltip's css has an absolute position of top 0 and left 0. When the link is hovered, it calculates it's position from the top of the document and uses that as the css top position for the tooltip.The problem is that on some pages, offset is calculating the link's position perfectly, and on others, it is around 50 pixels too many. I can't figure out why.$(".tiptrigger").mouseenter(function() { var s_id = $(this).attr('id'); var calc = $(this).offset().top; $("#tip-"+s_id).css({"margin-top": calc+"px"});}); 解决方案 Instead of absolute try to use fixed for your popup element. Than offset().top should work if you don't have any unnecessary padding applied to bodyIf still inaccurate instead of .offset().top give a chance to https://developer.mozilla.org/en-US/docs/Web/API/element.getBoundingClientRect 这篇关于jQuery偏移量在某些页面上无法正确计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 14:30