Skip to content

Instantly share code, notes, and snippets.

@nomadalex
Created October 27, 2011 15:59
Show Gist options
  • Save nomadalex/1319977 to your computer and use it in GitHub Desktop.
Save nomadalex/1319977 to your computer and use it in GitHub Desktop.
sinppet for docked menu.
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;
}
};
function scrollTop() {
return document.body.scrollTop || document.documentElement.scrollTop;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment