Skip to content

Instantly share code, notes, and snippets.

@sohelamin
Created March 19, 2015 06:44
Show Gist options
  • Save sohelamin/6fd918909ff51ae03024 to your computer and use it in GitHub Desktop.
Save sohelamin/6fd918909ff51ae03024 to your computer and use it in GitHub Desktop.
Stick Header Menu while scrolling down
<!DOCTYPE html>
<html>
<header>
<title>Stick Header</title>
<style>
.stick-header {
width: 100%;
z-index: 99999;
position: fixed;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</header>
<body>
<div class="header">
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
<script>
$(document).ready(function() {
var s = $("div.header");
var pos = s.position();
$(window).scroll(function() {
var windowpos = $(window).scrollTop();
//s.html("Distance from top:" + pos.top + "<br />Scroll position: " + windowpos);
if (windowpos >= pos.top) {
s.addClass("stick-header");
} else {
s.removeClass("stick-header");
}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment