javascript - How can I make my navbar stick to the bottom of an element upon reaching it? -
okay, i'm trying navbar stick bottom of 25px tall header have @ top of page. want navbar stick (become fixed — position: fixed
) when reaches bottom of header.
a link codepen set can found below. apologize css , javascript/jquery stuff you'll see.... it's part of site/design though.
i can't navbar (located @ bottom of screen) become fixed when reaches bottom of 25px (black-ish) header @ top of screen. i've tried 10+ solutions different places , none of them doing trick me....
what need add class nav first since have nav absolutely positioned in window find window height , minus nav , top bar have , add , remove classes there. following work:
$(window).on('scroll', function () { if ( $(window).scrolltop() >= $(window).height() - 105) { $( '#mainnav' ).addclass("scrolled"); }else{ $( '#mainnav' ).removeclass("scrolled"); } });
then css
#mainnav.scrolled { /* take place when navbar reaches set point on page */ position: fixed; z-index: 99; top:25px; left:0; }
normally use $('div').offset().top
instead of $(window).height() - 105
nav absolutely positioned in window , not in relative div cause inconsistent results find windows height , subtract height of nav , top bar. nav should fixed in correct position once scrolled bottom of top bar. here working codepen codepen
Comments
Post a Comment