I think this does what you want:
$(document).ready(function(){
var images = [
1,
"http://t2.uccdn.com/en/images/5/9/2/img_causes_of_fever_in_cats_9295_300_150.jpg",
"http://www.jimvysearks.co.uk/blog/wp-content/uploads/2014/06/duck_surfing-300x150.jpg",
"http://brightworkcoresearch.com/wp-content/uploads/2013/12/beauty-in-nature-178495_1280-300x150.jpg",
"http://www.educationworld.com/sites/default/files/Sun1.jpg"
];
$("#imgTag").attr("src",images[images[0]]);
(function loopImages() {
++images[0];
if (images[0] == images.length) {images[0] = 1;}
$("#imgTag").fadeOut(8000,function(){
$(this).attr("src",images[images[0]]).fadeIn(500,function(){loopImages();});
});
})();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="imageDiv">
<img src="" id="imgTag" />
</div>
jsfiddle: https://jsfiddle.net/9o2cw3hx/
- I changed your individual
img1-4
vars into an array containing all the images.
- At index
[0]
, the array also contains the index-number of the current image.
- This index-number is incremented every time
loopImages()
is invoked (by itself, after the src
is changed), in order to select the next image: ++images[0];
.
- After the last image in the array has been reached, the index-number is reset to the first image again:
if (images[0] == images.length) {images[0] = 1;}
.
- In order to get the function executed the first time, I enclosed it like this:
(function(){...})();
. That's called an IIFE. This way, you don't have to manually invoke the function after declaring it.
I removed the src
from your HTML and instead set it with jQuery, but that's obviously not necessary the way you set it using PHP (your way is better).
And I changed the images to literal strings for demo purposes, you can change those back to PHP.
OPTION 2
Alternatively, you can send the index-number along as an argument when you invoke the function:
(function loopImages(i) {
$("#imgTag").fadeOut(8000,function(){
$(this).attr("src",images[i]).fadeIn(500,function(){loopImages(++i<images.length?i:0);});
});
})(1);
- The first index in the
images
array, storing the index-number, is no longer necessary and can be removed (see the code snippet below).
- The two separate lines for incrementing the index-number, and the if-clause, are now combined into one inline ternary operator when the function invokes itself again:
loopImages(++i<images.length?i:0);
It works like this:
(1) It first increments i
and checks if that incremented value is still less than the array-length.
(2) If it is, that (incremented) i
is send along.
(3) If it isn't, 0
is send along, resetting the index-number to the first image in the array.
Personally, I like this option better, as it's more concise.
$(document).ready(function(){
var images = [
"http://t2.uccdn.com/en/images/5/9/2/img_causes_of_fever_in_cats_9295_300_150.jpg",
"http://www.jimvysearks.co.uk/blog/wp-content/uploads/2014/06/duck_surfing-300x150.jpg",
"http://brightworkcoresearch.com/wp-content/uploads/2013/12/beauty-in-nature-178495_1280-300x150.jpg",
"http://www.educationworld.com/sites/default/files/Sun1.jpg"
];
$("#imgTag").attr("src",images[0]);
(function loopImages(i) {
$("#imgTag").fadeOut(8000,function(){
$(this).attr("src",images[i]).fadeIn(500,function(){loopImages(++i<images.length?i:0);});
});
})(1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="imageDiv">
<img src="" id="imgTag" />
</div>
jsfiddle: https://jsfiddle.net/9o2cw3hx/1/