If this should not be a background image, you can put all the images in #wrapper, in <img> , it will work like a charm:
<div id="wrapper"> <img src="firstImage" class="imageClass"></img> <img src="secoundImage" class="imageClass"></img> <img src="thirdImage" class="imageClass"></img> </div>
then some style. Each image should be in one place, so add a position relative to #wrapper and set the absolute value to .imageClass:
#wrapper{ position: relative; } .imageClass{ position: absolute; top: 0; left: 0; display: none; }
display: none; will hide every image.
Now a few jQuery. To display the first image when loading the window, write this:
$(window).load(function() { $('.imageClass').eq(0).show(); });
with the .eq () "command, you can specify which element with the" .imageClass "class you want to use exactly. It starts with 0. After that, just do something like this:
function changeBackground() { var current = 0; //tells which image is currently shown if(current<$('.imageClass').length){ //loop that will show first image again after it will show the last one $('.imageClass').eq(current).fadeOut(500); current++; $('.imageClass').eq(current).fadeIn(500); } else { $('.imageClass').eq(current).fadeOut(500); current=0; $('.imageClass').eq(current).fadeIn(500); } } changeBackground(); setInterval(changeBackground, 3000); });
That should work, hope you enjoy it.
source share