I am trying to create a jquery 'for loop' that is used to get image urls from an array and use them to change a background of a div with class ".mobile".
The array is filled up with 3 urls. The problem is that the first and second urls are ignored and the third image is displayed 3 times. Also, I tried inserting an alert to see what the "i" variable contains and the alert comes up 3 times after each other, iterating from 0 to 2 without the code being executed before each alert.
Here is the code:
$(document).ready(function(){
var images = [ "url(images/image1.png)","url(images/image2.png)","url(images/image3.png)" ];
var i;
for (i = 0; i < images.length; i = i+1) {
$('.mobile').delay(2000).animate({'opacity': '0.0'}, 1000);
$('.mobile').css("background-image", images[ i ]);
$('.mobile').delay(2000).animate({'opacity': '1.0'}, 1000);
alert(i);
};
});
So to recap, I want to show each background image in the array with a delay before the next image appears.
I am still quite new to jQuery so any help would be greatly appreciated! :)