So, I'm trying to create an animated background image that will cycle through an array of images.
The idea also is that when you click on any navigation item on the page, the loop pauses.
When you press the home button, the loop should start again (from the current image).
This works in the current state, however, the cycle is not automatic when you restart, instead you must press the home button for each attenuation / slide / independently.
The script looks like this:
$(document).ready(function(){
var imgArr = new Array(
'img/slides/slide1.jpg',
'img/slides/slide2.jpg',
'img/slides/slide3.jpg');
var preloadArr = new Array();
var i;
for(i=0; i < imgArr.length; i++){
preloadArr[i] = new Image();
preloadArr[i].src = imgArr[i];
}
var currImg = 1;
var IntID = setInterval(startSlider, 4000);
function startSlider(){
$('.mainbg').animate({opacity: 0}, 1200, "easeInOutExpo", function(){
$(this).css('background','url(' + preloadArr[currImg++%preloadArr.length].src + ') no-repeat center center fixed');
$(this).css({'background-size': 'cover' , '-webkit-background-size': 'cover' , '-moz-background-size': 'cover' , '-o-background-size': 'cover' ,});
}).animate({opacity: 1}, 1200, "easeInOutExpo");
}
function stopSlider() {
clearInterval(IntID);
}
$(".topnav ul li a").click(stopSlider);
$("#home").click(startSlider);
});
If someone could direct me in the right direction, that would be very grateful! Best regards, Casper.