首页 > 解决方案 > 为什么 offset() 依赖于滚动

问题描述

根据文档

offset() 方法设置或返回所选元素相对于文档的偏移坐标。

所以offset()不应该依赖于滚动中的位置。

但是,在我的代码中:我的 html 格式为:

...
<div class="paragraph"><div class="image"><img src="Pictures/2018-06-17_12-04-32.png">this is a picture</div> : text</div>
<div class="paragraph"><div class="image"><img src="Pictures/2018-05-16_16-03-00.png">this is another picture</div> : other text</div>
...

脚本是:

$("div.image").mouseover(function(){
    var pos = $(this).offset();
    console.log('pos_top :', pos.top);

每次滚动时,将鼠标悬停在图片上都会显示不同的数字pos.top。这怎么可能?

标签: jqueryoffset

解决方案


推荐阅读