Skip to content

Instantly share code, notes, and snippets.

@ryandejaegher
Created June 14, 2020 10:55
Show Gist options
  • Select an option

  • Save ryandejaegher/17c6f3144864657a5e46ade6655890a4 to your computer and use it in GitHub Desktop.

Select an option

Save ryandejaegher/17c6f3144864657a5e46ade6655890a4 to your computer and use it in GitHub Desktop.
<script>
var mainHeader = document.querySelector('header');
var mainHeaderHeight = mainHeader.getBoundingClientRect().height
var addBg = function(){
mainHeader.classList.add('new-bg');
}
var removeBg = function(){
mainHeader.classList.remove('new-bg');
}
window.addEventListener('scroll', () => {
if(window.pageYOffset > mainHeaderHeight) {
requestAnimationFrame(addBg);
} else {
requestAnimationFrame(removeBg);
}
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment