I'm trying to get background Images to fadeIn / fadeOut when the user scrolls down the page. In the long run I'm shooting for a time lapse effect using several background images. I've got the background images changing on scroll, but I can't seem to get them to fade from one to the next.
Here is what I've got so far - jsFiddle
$(document).scroll(function () {
if ($(this).scrollTop() > 1) {
$('body').css({
backgroundImage: 'url("http://s3.amazonaws.com/dfc_attachments/images/3237846/sun1_web.png")'
});
}
if ($(this).scrollTop() > 250) {
$('body').css({
backgroundImage: 'url("http://s3.amazonaws.com/dfc_attachments/images/3237850/sun2_web.png")'
});
}
if ($(this).scrollTop() > 500) {
$('body').css({
backgroundImage: 'url("http://s3.amazonaws.com/dfc_attachments/images/3237854/sun3_web.png")'
});
}
if ($(this).scrollTop() > 750) {
$('body').css({
backgroundImage: 'url("http://s3.amazonaws.com/dfc_attachments/images/3237858/sun4_web.png")'
});
}
});
Used this with a bit of bug shooting for a vertical scrolling site with images replaced at each "level" as the user scrolls down. I was trying to use SuperScrollorama but after kicking it around for an hour I gave up. This is great for simple vertical "story telling" style sites.
So, firstly, in the header:
And the HTML
You need to edit the "(y > xxxx)" parts in the header to fade in at which ever point your page needs it. I added up the two images above the swap and subtracted 400 to have it fade in at the bottom of the screen.
This may seem silly to most coders but I hope it helps any other noob like myself to get this right.
Here's how it works, for any one else having this problem.
jsFiddle
You can't animate the
background-image
property. Instead, render the images with standard<img>
tags and fade those in on top of one another.