Sticky Header - Scroll - CSS / jQuery

I want to create a sticky headline. Each time the user scrolls down and the original heading leaves, then the heading β€œsticky” should begin.

I am currently using 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 adds a sticky class whenever the user simply scrolls, not when the original title needs to be removed.

Hi

+5
source share
2 answers

Wrap it divwithid="whateveryouwannacallit"

and install:

#whateveryouwannacalltit{
position:fixed;
top:0;
left: 0;
width:100%;
}
+3
source

You don’t really need packaging. Here is the code

$(document).ready(function() {
  var stuck = $('#header');
  var start = $(div).offset().top;
  $.event.add(window, "scroll", function() {
    var p = $(window).scrollTop();
    $(stuck).css('position',((p)>start) ? 'fixed' : 'static');
    $(stuck).css('top',((p)>start) ? '0px' : '');
  });
});

: http://viralpatel.net/blogs/scroll-fix-header-jquery-facebook/

+1

All Articles