与FF和IE相比,jQuery在Chrome中返回错误的元素位置

时间:2011-08-23 23:06:34

标签: jquery css google-chrome scroll height

我正在编写一个函数来测试滚动条位置与我存储在数组中的预定义元素位置。页面上可以有任意数量的此元素类型,这就是我使用for循环测试滚动条位置的原因。

此代码在Firefox中运行良好,但由于某种原因,jQuery为Chrome中的每个.post-info($ pi)项目返回完全不同的位置,甚至得到$(文档)。严重错误。这是我的javascript或我的CSS的问题吗?我会假设我的JS,因为我已经测试了我的位置:相对于我的CSS中的大多数元素。

这是我正在运行的jQuery:

(function(){
var i,
st,
length,
arrPos = []

  $wn = $(window);
  $pi = $('.post-info');

  $pi.each(function(){
    arrPos.push($(this).offset().top - 80); // offset -80 so new item sticks quicker.
  });
  height = $(document).height();
  arrPos.push(height); // add bottom position of document to array for last item.

  length = arrPos.length;           
  $wn.scroll(function(){ //Scroll Event Listener
    st = $wn.scrollTop();
    for(i=0; i<=length; i++){
       // i = index, n = next
       n = i+1;
       if(st > arrPos[i] && st < arrPos[n]) _stick(i);
    }
});

非常感谢。

1 个答案:

答案 0 :(得分:1)

Google Chrome在$(文档).ready之后设置高度和宽度 如果您有任何图片,那么在此特定时刻您的网页尺寸将不合适。首先尝试预先加载图像,然后再调用功能。 希望这有帮助:)

相关问题