【问题标题】:jQuery returning wrong positions of elements in Chrome compared to FF and IE与 FF 和 IE 相比,jQuery 在 Chrome 中返回错误的元素位置
【发布时间】:2011-11-02 08:55:45
【问题描述】:

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

这段代码在 Firefox 中运行良好,但由于某种原因,jQuery 在 Chrome 中为我的每个 .post-info ($pi) 项目返回完全不同的位置,甚至导致 $(document).height 错误。这是我的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);
    }
});

非常感谢。

【问题讨论】:

    标签: jquery css google-chrome scroll height


    【解决方案1】:

    谷歌浏览器在 $(document).ready 之后设置高度和宽度 如果您有任何图像,那么您的页面尺寸在这个特定时刻不会是正确的。尝试先预加载您的图像,并在完成后调用您的函数。 希望对您有所帮助:)

    【讨论】:

    • 感谢您的建议,但我发现问题是我的图像都没有设置特定的高度 - 我已经添加了,现在 Chrome 承认它们的存在,一切都很好!谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-03-23
    • 2016-02-16
    • 2012-12-10
    • 1970-01-01
    • 2012-10-13
    • 2012-02-14
    • 2011-08-05
    • 1970-01-01
    相关资源
    最近更新 更多