Skip to content

Instantly share code, notes, and snippets.

@YohannParis
Created December 13, 2016 19:58
Show Gist options
  • Save YohannParis/5968f0537fd01cb1f855eb7115da9e44 to your computer and use it in GitHub Desktop.
Save YohannParis/5968f0537fd01cb1f855eb7115da9e44 to your computer and use it in GitHub Desktop.
Easy and quick way to add a sticky element.
var header = document.querySelector('.header');
var origOffsetY = header.offsetTop;
function onScroll(e) {
window.scrollY >= origOffsetY ? header.classList.add('sticky') :
header.classList.remove('sticky');
}
document.addEventListener('scroll', onScroll);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment