jquery 2.2: navbar fixed on scroll not working with vh-unit -
i found on topic doesn't me out here: how implement scroll fixed effect?
i'm trying implement navbar, sticks top when user scrolled on header-section can seen here: https://teamtreehouse.com/community/forum-tip-create-a-sticky-navigation-with-css-and-jquery-2
however, since i'm using vh , vw units in project, got problems getting jquery work. it's working fine, if use px like
... if ($(this).scrolltop() > 400 ) ...
but
... if ($(this).scrolltop() > $("#slider_part").height ...
it's not working.
see code below. , in advance answers!
my html: <header id="slider_part"> <div>...</div> </header> <div id="main-nav" class="navbar"> <nav>...</nav> </div> css: #slider_part { height: 85vh; width: 100%; overflow:hidden; position: fixed; top: 0; z-index: -100; } #main-nav { position: relative; background: #db2f27; -webkit-box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.12); box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.12); z-index: 150; border: none !important; } .navbar { height: 15vh; width: 100vw; border-radius: 0 !important; margin-bottom: 0 !important; } .main-nav-scrolled { width: 100vw; position: fixed !important; top: 0; } jquery: var mn = $(".navbar"); $(window).scroll(function(){ if ($(this).scrolltop() > $("#slider_part").height ) { mn.addclass("main-nav-scrolled"); } else { mn.removeclass("main-nav-scrolled"); } });
check errors in console, have missed ()
.height()
method:
if ($(this).scrolltop() > $("#slider_part").height() ) {
another suggestion use .toggleclass()
method:
$(window).scroll(function(){ mn.toggleclass('main-nav-scrolled', $(this).scrolltop() > $("#slider_part").height()); });
in above example class added when condition true , removed when false.
Comments
Post a Comment