-3

I'd like to make a long website with lots of text more convinient by making the h1 or h2 element that would have just gone out of view (when scrolling down) sticky at the top until the next headline is about to go out of view and become sticky.

I believe I've seen this on some websites, but I don't know how to call it and can't find an example.

I'm using Bootstrap and JQuery, so a solution that works with either of them would be perfect.

Klaus Stadler
  • 395
  • 3
  • 17

1 Answers1

0
$(window).on("scroll",function(){
/*Check if window scroll Top reaches menu position */
    if ($(window).scrollTop() >= 100) {
        $('div.menu-class').addClass('fixed');
        }
    else {
       $('div.header').removeClass('fixed');
        
    }
});

Or You can check if the menu element top position equals window scroll top.

 var window_top_position = $(window).scrollTop();
 var menuElement = $('.menu-class');
 var Menu_top_position = $menuElement.offset().top;

Then check the condition and add 'fixed' if condition satisfies, else remove 'fixed' class

Dont forget to add CSS for fixed.

.fixed{
position:fixed;
top:0;
}
General Grievance
  • 4,555
  • 31
  • 31
  • 45