Skip to content

Instantly share code, notes, and snippets.

@nblenke
Created October 1, 2014 19:32
Show Gist options
  • Save nblenke/ca88e47a4861b6bb6e07 to your computer and use it in GitHub Desktop.
Save nblenke/ca88e47a4861b6bb6e07 to your computer and use it in GitHub Desktop.
Fixed header on scroll
(function () {
var el = document.querySelector('#header'),
className = 'fixed',
offset = (el.offsetTop + el.offsetHeight),
scrollTop = function () {
return document.body.scrollTop || document.documentElement.scrollTop;
};
if (scrollTop() > offset) {
el.className = className;
}
window.onscroll = function() {
if (scrollTop() > offset) {
if (!el.className.length) {
el.className = className;
}
} else {
if (el.className.length) {
el.className = '';
}
}
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment