Created
December 14, 2011 19:27
-
-
Save erikformella/1478072 to your computer and use it in GitHub Desktop.
nav bar follows page scroll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onload = function() | |
{ | |
var headers = document.querySelectorAll('#docs h2, #guide h1'); | |
var menu = document.getElementById('menu'); | |
var init = menu.offsetTop; | |
var docked; | |
window.onscroll = function () | |
{ | |
if (!docked && (menu.offsetTop - scrollTop() < 0)) | |
{ | |
menu.style.top = 0; | |
menu.style.position = 'fixed'; | |
menu.className = 'docked'; | |
docked = true; | |
} | |
else if (docked && scrollTop() <= init) | |
{ | |
menu.style.position = 'absolute'; | |
menu.style.top = init + 'px'; | |
menu.className = menu.className.replace('docked', ''); | |
docked = false; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment