Make scrolling sidebar stop at footer

前端 未结 5 1185
陌清茗
陌清茗 2020-12-09 06:20

I\'m currently using the following:

http://jsfiddle.net/0mLzseby/469/

To make my sidebar follow down the page. I have quite a large footer though and I\'d li

5条回答
  •  猫巷女王i
    2020-12-09 07:16

    You forgot to add class, if we are in the footer, and refrech the page, then, the sidebar won't show :

    function sticky_relocate() {
        var window_top = $(window).scrollTop();
        var footer_top = $("#footer").offset().top;
        var div_top = $('#sticky-anchor').offset().top;
        var div_height = $("#sticky").height();
    
        var padding = 20;  // tweak here or get from margins etc
    
        if (window_top + div_height > footer_top - padding) {
            $('#sticky').addClass('stick'); //////// here is to get fixed when we refrech page when we are in the footer
            $('#sticky').css({top: (window_top + div_height - footer_top + padding) * -1})
    }     else if (window_top > div_top) {
            $('#sticky').addClass('stick');
            $('#sticky').css({top: 0})
        } else {
            $('#sticky').removeClass('stick');
        }
    }
    

提交回复
热议问题