Skip to content

Instantly share code, notes, and snippets.

@erikformella
Created December 14, 2011 19:27
Show Gist options
  • Save erikformella/1478072 to your computer and use it in GitHub Desktop.
Save erikformella/1478072 to your computer and use it in GitHub Desktop.
nav bar follows page scroll
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