【问题标题】:jquery scrollToFixed stopped when it reached the very top of the pagejquery scrollToFixed 在到达页面顶部时停止
【发布时间】:2023-03-04 03:58:01
【问题描述】:

jquery scrollToFixed 在到达页面的最顶部时停止,它只出现在应该固定在最顶部的 div 的顶部。

Here is what I have so far.

var arr = new Array();
$('div[id^="post"]').each(function()
{
   // do something with it...
    var id = $(this).attr("id").slice(5);
    //alert(id);

    arr.push(id);

   $(this).css('background-color', 'green');
});

     for (i = 0; i < $('div[id^="post"]').length; i++) {
      //alert(arr[i+1]);

            $('#float-'+arr[i]).scrollToFixed({
        limit: $('#post-'+arr[i+1]).offset().top - $('#float-'+arr[i]).height()-220
    });
    }​

我正在使用 jquery scrollToFixed 插件。

我正在尝试在 9gag.com 上实现相同的效果,在屏幕上向下滚动多个侧边栏 div,I got the idea here

当您滚动页面时,右侧 div 应该固定,直到到达左侧 div 的末尾,但由于某种原因,右侧 div 固定在页面的最顶部,我有顶部(广告和navigation) div 一直设置为固定。

【问题讨论】:

    标签: javascript jquery html css css-position


    【解决方案1】:

    如果我理解正确,那么会发生什么。 jQuery 插件 (scrollToFixed) 不知道您希望元素位于另一个元素下方,因此您必须自己做。

    最简单的方法是给#float-63(imo 不是一个很好的ID)一个margin-top,它等于它应该位于其下方的元素的高度。 70px 似乎可以正常工作。

    这样做的问题是,即使它没有“固定”,它也会有 70px 的顶部边距。插件在修复时似乎不会向元素添加任何类型的类,但如果你知道 jQuery,那应该很容易添加。

    那么你可以简单地做#float-63.scroll-to-fixed {margin-top: 70px}

    【讨论】:

    • 我正在使用 wordpress,每篇文章都有唯一的 id,因此 #float-63。我不太了解 jquery,我花了将近 5 个小时才得到我现在所拥有的。我在哪里放置这个#float-63.scroll-to-fixed {margin-top: 70px}。我会继续搜索,并通过 jquery api。如果我找到了,会在这里发布答案
    • 我找到了答案,感谢您为我指明了正确的方向。现在我只需要找到一种方法将固定 div 的位置设置为相对,一旦到达图片的末尾,我终于可以将它放在生产站点上
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 2020-06-01
    • 2011-07-24
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多