Struggling to make my background image change while scrolling has been a challenge for me. I've tried different solutions suggested for similar questions, but so far no success - it only displays the initial background image.
The setup involves having the background positioned as fixed and multiple divs, each with the same size as the viewport and a top margin equal to the height of the viewport. This means that the user will see the background image only after scrolling past each div. What I want is for the background image to switch after passing each div.
Below is the code snippet:
$(window).scroll(function() {
var windowY = $(window).height();
var scrolledY = $(window).scrollTop();
var image_url = '/images/image1.jpg';
if (scrolledY > windowY) {
image_url = '/images/image2.jpg';
}
$('body').css('background', "url(" + img_url + ")");
});
Any help or suggestions would be greatly appreciated!