Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sticky Header - Scroll - CSS / jQuery

I wish to create a sticky header. Everytime that the user scrolls down AND the original header goes away, then the "sticky" header should kick in.

I currently use this:

$(function(){
    // Check the initial Poistion of the Sticky Header
    var stickyHeaderTop = $('#sticky').offset().top;
    $(window).scroll(function(){
        if( $(window).scrollTop() > stickyHeaderTop ) {
            //$('#sticky').css({position: 'fixed', top: '0px', float: 'right'});
            $('#sticky').addClass("sticky");
        } else {
            $('#sticky').removeClass("sticky");
        }
    });
});

Although, the current one add the class "sticky" whenever a user just does a scroll, and not when the original header should be gone.

Regards

like image 307
oliverbj Avatar asked Oct 09 '22 10:10

oliverbj


1 Answers

Wrap him with a div with id="whateveryouwannacallit"

and set:

#whateveryouwannacalltit{
position:fixed;
top:0;
left: 0;
width:100%;
}
like image 188
Erick Avatar answered Oct 13 '22 11:10

Erick