How to determine the final distance of an element from the footer?

Asked

Viewed 430 times

5

In the case in question would be a sidebar, which from a certain height of scrolling from the top, receives position:Fixed, but as the scroll continues it is hidden behind the footer of the site. How could I determine that this sidebar from a certain distance from the footer stops? (the footer has about 490px).

1 answer

4


You can do it:

$(function(){
    $(window).scroll(function(){

    var div = $('#div');
    var space = $(document).height() - (div.offset().top + div.height());

        if(space <= 'quantidade que você quer'){
            div.css('position','relative');
        }    

    });
});

ai inside the if you set the css as you want...

  • 1

    div.offset().top * 2? In fact, why double the offset?

  • 1

    Corrected, actually the sum is with the . height()!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.