Skip to content

Instantly share code, notes, and snippets.

@daangemist
Created December 20, 2012 21:58
Show Gist options
  • Save daangemist/4348909 to your computer and use it in GitHub Desktop.
Save daangemist/4348909 to your computer and use it in GitHub Desktop.
Lock the menu header in place when the page starts scrolling. Taken from www.lesscss.org, the docked CSS class makes the background semi-transparent.
window.onload = function () {
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