Looking to create a cool effect where the background color of a fixed header changes as the user scrolls down a full-page block website. Managed to get it mostly working in this Pen, but struggling with determining how much has been scrolled to trigger the change.
A bit more detail: Need the switch to happen at 400px of scroll. The different background colors are stored in an array for easy fetching. Here's my jQuery code snippet:
$(document).ready(function(){
var bgArray = ["#252525","#333333","#454545","#777777"];
var scrollHeight = 400;
var scrolled = $(window).scrollTop(); // What does this exactly measure?
$(window).scroll(function() { // Can these conditions be combined into one?
if(scrolled < scrollHeight) {
$('header').css('background', bgArray[0]);
}
if(scrolled > scrollHeight) { // i.e more than 400px
$('header').css('background', bgArray[1]);
}
// and so on (800, 1200...)
})
})
Check out the Pen for the complete code. Any ideas or suggestions would be really helpful!