Skip to content

Instantly share code, notes, and snippets.

@febridev
Created March 23, 2017 07:17
Show Gist options
  • Save febridev/c2ada464364242639c7ac797f815b481 to your computer and use it in GitHub Desktop.
Save febridev/c2ada464364242639c7ac797f815b481 to your computer and use it in GitHub Desktop.
auto-fixed-navbar-then-scroll.html
<style>
menu {
background:#345;
color:#FFF;
height:2em;
padding:.5em;
position:absolute;
top:150px;
width:100%;
}
.fixed {
position:fixed;
top:0;
}
</style>
<div class="menu">Menu</div>
<br />
<br />
<br />
<br />
<br />
<br />
<script>
/* Dynamic top menu positioning
*
*/
var num = 150; //number of pixels before modifying styles
$(window).bind('scroll', function () {
if ($(window).scrollTop() > num) {
$('.menu').addClass('fixed');
} else {
$('.menu').removeClass('fixed');
}
});
//USE SCROLL WHEEL FOR THIS FIDDLE DEMO
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment