javascript - Navbar with fixed position staying on the top while header disappear -
i want make navbar fixed position. @ top of page navbar should under header , after scrolling down when header no longer visible navbar should @ top of page. how can that? when try after scrolling down between navbar , top of page still height of header(even though no longer visible).
here css:
header{ background-color: red; width: 100%; height: 100px; } nav{ position: fixed; float:left; height: 100px; width: 100px; margin-left: 10px; margin-right: 50px; margin-top:50px; background-color: green; } main{ background-color: blue; height: 1500px; margin-left:15%; margin-right:5%; margin-top:50px; }
and jfiddle: https://jsfiddle.net/pg2kwk5e/
you can add class nav
element javascript after scrolling amount.
i've used jquery
it's faster , easier show in action.
i'm adding class .fixedtop
nav
after window scrolls more 150 pixels, class has top:0;margin0;
move absolute positioned element top , remove margin set before.
code:
var $nav = $("nav"), $(window).scroll(function() { if($(this).scrolltop() > 150) { $nav.addclass('fixedtop'); } else { $nav.removeclass('fixedtop'); } })
css:
.fixedtop { top: 0; margin: 0 !important; }
Comments
Post a Comment