简体   繁体   English

如何阻止固定的侧边栏进入页脚?

[英]How to stop a fixed sidebar from going in the footer?

I'm building a website. 我正在建立一个网站。 http://check.topicwine.com Have a look to see my work. http://check.topicwine.com看看我的工作。

I want to make a static sidebar. 我想制作一个静态的侧边栏。 I'm using the code: 我正在使用代码:

$(function() {
        var offset = $("#ad-wrapper").offset();
        var topPadding = 60;
        $(window).scroll(function() {
            if ($(window).scrollTop() > offset.top) {
                $("#ad-wrapper").stop().animate({
                    marginTop: $(window).scrollTop() - offset.top + topPadding
            });
            } else {
                $("#ad-wrapper").stop().animate({
                marginTop: 0
            });
        };
});
});

The sidebar comes along, but it also goes where it shouldn't. 边栏会出现,但它也会出现在不应该出现的地方。 I mean, it enters the footer as well. 我的意思是,它也进入页脚。 Rather, it overlaps the footer. 而是,它与页脚重叠。

I want it to stop next to the grid. 我希望它停在网格旁边。

Thanks, in advance. 提前致谢。 :) :)

Set a limit for the top margin, since the sidebar can't go past the $('#main') element. 由于边栏不能超过$('#main')元素,因此请为顶部边距设置一个限制。

$(function() {
    var offset = $("#ad-wrapper").offset();
    var topPadding = 60;
    $(window).scroll(function() {
        var scrollTop = $(window).scrollTop(); // Store this for convenience
        if (scrollTop > offset.top) {
            var newMarginTop = scrollTop - offset.top + topPadding;
            // The sidebar can only go so far!
            var marginLimit = $('#main').height() + $('#main').offset().top - offset.top - $("#ad-wrapper").height();
            if (newMarginTop > marginLimit) 
                newMarginTop = marginLimit;
            $("#ad-wrapper").stop().animate({
                marginTop: newMarginTop 
            });
        } else {
            $("#ad-wrapper").stop().animate({
                marginTop: 0
            });
        }
    });
});

Add overflow:hidden to div#content. overflow:hidden添加到div#content。 This way we will get the proper height of the content div. 这样,我们将获得内容div的适当高度。
Now $('#content').height() + $('#content').offset().top is the maximum distance the sidebar should move. 现在$('#content').height() + $('#content').offset().top是侧边栏应移动的最大距离。 Which means, the sidebar's offset.top + height should not go more than this. 这意味着,侧边栏的offset.top + height不应超过此值。 Add this check in your scroll handler 将此检查添加到滚动处理程序中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM