Skip to content

Instantly share code, notes, and snippets.

@eltiare
Created March 6, 2011 07:46
Show Gist options
  • Select an option

  • Save eltiare/857123 to your computer and use it in GitHub Desktop.

Select an option

Save eltiare/857123 to your computer and use it in GitHub Desktop.
An easy way to rotate images in a slideshow via jQuery
<html>
<head>
<title>Simple Slideshow</title>
<style type='text/css'>
#slideshow { position: relative; }
#slideshow img { position: absolute; top: 0; left: 0; }
</style>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
var image_counter = -1;
$(window).load(function() {
rotate_images();
setInterval(rotate_images, 7000);
});
function rotate_images() {
var images = $('#slideshow img');
image_counter = ++image_counter % images.length;
images.each(function(i, img) {
if (i == image_counter) {
$(img).css({zIndex : 4});
$(img).animate({ opacity : 1 }, 2000).animate({ zIndex : 5}, 200);
} else {
$(img).animate({ opacity : 0 }, 2000).animate({ zIndex : 0}, 200);
}
});
}
</script>
</head>
<body>
<div id="slideshow">
<img src="image1.jpg" alt="">
<img src="image2.jpg" alt="">
<img src="image3.jpg" alt="">
<img src="image4.jpg" alt="">
</div>
</body>
</html>
@eltiare

eltiare commented Mar 6, 2011

Copy link
Copy Markdown
Author

Haha! My turn to miss something. Very good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment