固定浮动元素在页脚处停止

发布于 2024-11-08 21:46:54 字数 845 浏览 4 评论 0原文

我使用了 http://jqueryfordesigners.com/fixed-floating-elements 中的代码滚动到某个点后浮动元素。这是我正在开发的网站: http://bodyecology.com/articles/gut-type -update

如您所见,滚动时右列变得固定,但在页脚处重叠。我怎样才能让它停在页脚上方?

目前使用:

<script>
    jQuery(document).ready(function () {  
    var top = jQuery('#news_sidebar').offset().top - parseFloat(jQuery('#news_sidebar').css('marginTop').replace(/auto/, 0));
    jQuery(window).scroll(function (event) {

        var y = jQuery(this).scrollTop();

        if (y >= top) {

        jQuery('#news_sidebar').addClass('fixed');

        } else {

       jQuery('#news_sidebar').removeClass('fixed');
    }
  });
 });
</script>

I've used the code from http://jqueryfordesigners.com/fixed-floating-elements to float an element after scrolling to a certain point. Here is the site I'm working on: http://bodyecology.com/articles/gut-type-update

As you can see, the right column becomes fixed when scrolled, but overlaps at the footer. How can I cause it to stop at just above the footer?

Currently Using:

<script>
    jQuery(document).ready(function () {  
    var top = jQuery('#news_sidebar').offset().top - parseFloat(jQuery('#news_sidebar').css('marginTop').replace(/auto/, 0));
    jQuery(window).scroll(function (event) {

        var y = jQuery(this).scrollTop();

        if (y >= top) {

        jQuery('#news_sidebar').addClass('fixed');

        } else {

       jQuery('#news_sidebar').removeClass('fixed');
    }
  });
 });
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫罗兰の梦幻 2024-11-15 21:46:54

这个小提琴可以满足您的需求:

http://jsfiddle.net/ZczEt/9/

这里是处理右侧浮动元素 $('#summary') 的 javascript:

$('#summary').scrollToFixed({
    marginTop:
        $('.header').outerHeight() + 10,
    limit:
        $('.footer').offset().top -
        $('#summary').outerHeight() -
        10
});

这是 jquery 插件及其来源:

https://github.com/bigspotteddog/ScrollToFixed

This fiddle does what you are looking for:

http://jsfiddle.net/ZczEt/9/

Here is the javascript that handles the floating element $('#summary') on the right:

$('#summary').scrollToFixed({
    marginTop:
        $('.header').outerHeight() + 10,
    limit:
        $('.footer').offset().top -
        $('#summary').outerHeight() -
        10
});

Here is the jquery plugin and its source:

https://github.com/bigspotteddog/ScrollToFixed

若言繁花未落 2024-11-15 21:46:54

我有和你一样的代码来浮动我的div
来自同一篇文章“Fiding-Floating-Elements”

并且与重叠页脚有同样的问题,对我有用的唯一解决方案(我是一个 javascript newvie)是使用以下代码将 div 从页脚中拉开:

$(window).scroll(function () {

            // distance from top of footer to top of document
            footertotop = ($('#footer').position().top);
            // distance user has scrolled from top, adjusted to take in height of sidebar (570 pixels inc. padding)
            scrolltop = $(document).scrollTop() + 570;
            // difference between the two
            difference = scrolltop - footertotop;

            // if user has scrolled further than footer,
            // pull sidebar up using a negative margin

            if (scrolltop > footertotop) {

                $('#cart').css('margin-top', 0 - difference);
            }

            else {
                $('#cart').css('margin-top', 0);
            }


        });

这对我很有帮助,我希望其他人也会发现它有帮助:)
我已经在我的网站上实现了它,以便用户可以获得完整文章视图

i have the same code as you to float my div
from the same post "Fixing-Floating-Elements"

and had the same problem as you have with the overlapping footer, the ONLY solution working for me (i'm a javascript newvie) was pulling the div away from the footer with this code:

$(window).scroll(function () {

            // distance from top of footer to top of document
            footertotop = ($('#footer').position().top);
            // distance user has scrolled from top, adjusted to take in height of sidebar (570 pixels inc. padding)
            scrolltop = $(document).scrollTop() + 570;
            // difference between the two
            difference = scrolltop - footertotop;

            // if user has scrolled further than footer,
            // pull sidebar up using a negative margin

            if (scrolltop > footertotop) {

                $('#cart').css('margin-top', 0 - difference);
            }

            else {
                $('#cart').css('margin-top', 0);
            }


        });

This was helpful to me i hope someone else will find it helpful :)
I've implemented it on my site so the users can get full articles view

这个俗人 2024-11-15 21:46:54

我可能是错的,但这似乎是右侧边栏中内容的大小。它会重叠,因为它太大,当您滚动到底部时不会重叠。

您可以在底部设置与顶部相同的止损并重新定位。当您滚动到顶部时,它会重新定位自己。页脚也尝试一下。

I could be wrong, but it appears to be the size of the content inside the right sidebar. It overlaps because its too big not to when you scroll to the bottom.

You could put the same stop and re-position at the bottom as you do on top. When you scroll to the top, it re-positions itself. Try that for the footer as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文