I'm using a simple piece of jQuery to rotate through (and randomize) a series of images. I'm wondering how difficult it would be to adjust it to rotate through background images that are defined by a class.
The script looks like this:
<script type="text/javascript">
jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
random: function(a, i, m, r) {
if (i == 0) {
jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
};
return i == jQuery.jQueryRandom;
}
});
$(function() {
$('#slideshow img').not(':random').hide(); //hide all images except one initially
setInterval(function(){
$('#slideshow img:visible').fadeOut('slow')
.siblings('img:random').fadeIn('slow') //find a random image
.end().appendTo('#slideshow');},
9000); //2 second interval
});
</script>
HTML is:
<div id="slideshow">
<img src="banner1.jpg" />
<img src="banner2.jpg" />
<img src="banner3.jpg" />
<img src="banner4.jpg" />
<img src="banner5.jpg" />
</div>
CSS
<style type="text/css">
#slideshow {
width: 100%;
position: relative;
}
#slideshow img {
top: 0;
left: 0;
width: 100%;
height: auto;
position: absolute;
}
</style>
So, what i'd like to do, but can't figure out is have the images rotate as a background applied to a class. So, instead of having all the images defined in the html, the would rotate through a class like this:
.slideshow {
background: url(banner1.jpg) no-repeat bottom center;
}
Is this possible with some adjustments to the script above? Any pitfalls in this method?
Thanks!