I am trying to get one background image to fade into another using a very simple if statement. Currently my background images do not fade into one and another, they first fade onto a white background and then the other image fades in after, I would however like them to fade into each other directly. Secondly there seems to be an error with my images displaying, so when the the limit of the array (1) is reached in the if statement then it will set a simple currentBackground variable to 0, meaning the backgrounds should start from 0 in the array, however, this does not happen and sometimes the image same image is displayed repetitively.
$(window).load(function() {
var images = ['img/fitness-bg-2.jpg','img/runner-header.jpg'];
var i = 0;
function changeBackground() {
$('#header').fadeOut(250, function(){
$('#header').css('background-image', function () {
if (i >= images.length) {
i = 0;
}
return 'url(' + images[i++] + ')';
});
$('#header').fadeIn(500);
})
}
changeBackground();
setInterval(changeBackground, 5000);
});