Skip to content

Instantly share code, notes, and snippets.

@g3luka
Created April 2, 2020 16:23
Show Gist options
  • Save g3luka/49ba644eeb699b96f0e8976495247fec to your computer and use it in GitHub Desktop.
Save g3luka/49ba644eeb699b96f0e8976495247fec to your computer and use it in GitHub Desktop.
Element sticky fixed
<p style="margin-bottom:100px;">Scroll this page.</p>
<div class="sticky"><h3>Super amazing header</h3></div>
var sticky = document.querySelector('.sticky');
var origOffsetY = sticky.offsetTop;
document.addEventListener('scroll', function(e) {
window.scrollY >= origOffsetY ? sticky.classList.add('fixed') :
sticky.classList.remove('fixed')
});
.fixed { position: fixed; top: 0; width: 100%; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment