JQuery Image Switcher

7th December 2009

Using something like CrossSlide is fine if you want fancy effects in your image translations, but for more simple effects you can use a single function to simply swap one image for another. First, lets create some simple markup that will allow us to display some images.

  1.  

When I created this script one of the requirements was the need for different sized images to be added without destroying the layout of the rest of the page. For this reason I also added some code to collect the height and width of all of the image and use the maximum value as the height and width of the containing div. The following little snippet is used to get the maximum value of an array using the notation array.max().

  1. Array.prototype.max = function() {
  2. return Math.max.apply(null, this);
  3. };

The code to create the image slideshow is quite simple. What is happening here is that every instance of a div with the class slideshow on the page is found that and some setup routines are run on them and their contents. The set up involves setting the initial and final image, setting some default styles for the images and then finding out the maximum height and width of the images in the div. The maximum height and width of the images is used to set the height and width of the containing div. A function is then created that will be used to swap the images and a call to setTimeout() added so that an interval can be created. The advanceSlideshow() function just swaps the currently selected image with the next in the list. This interval is currently set at 5000, which means that every 5 seconds the image will fade from one to the next.

  1. $(document).ready(function(){
  2. $("div.slideshow").each(function()
  3. {
  4. var slideshow = $(this);
  5. var selectedImage = 0;
  6. var maxSelectedImage = slideshow.find("img").length - 1;
  7.  
  8. slideshow.css("position", "relative");
  9.  
  10. slideshow.find("img")
  11. .css("position", "absolute")
  12. .css("top", 0)
  13. .css("left", 0)
  14. .hide();
  15.  
  16. slideshow.find("img:eq("+selectedImage+")").show();
  17.  
  18. var height = new Array();
  19. var width = new Array();
  20. slideshow.find("img").each(function(i){
  21. var currentImg = $(this);
  22. height[i] = currentImg.height();
  23. width[i] = currentImg.width();
  24. i++;
  25. });
  26.  
  27. slideshow.css('height', height.max());
  28. slideshow.css('width', width.max());
  29.  
  30. var advanceSlideshow = function()
  31. {
  32. var newSelectedImage = selectedImage + 1;
  33. if(newSelectedImage > maxSelectedImage)
  34. newSelectedImage = 0;
  35.  
  36. slideshow.find("img:eq("+selectedImage+")").fadeOut();
  37. slideshow.find("img:eq("+newSelectedImage+")").fadeIn();
  38.  
  39. selectedImage = newSelectedImage;
  40.  
  41. setTimeout(advanceSlideshow, 5000);
  42. };
  43. setTimeout(advanceSlideshow, 5000);
  44. });
  45. });

The good thing about using JQuery for this code is that you are not stuck with the images fading in or out. It is perfectly possible to use slideUp() and slideDown() or even animate() to change the way in which one image is changed for the other.

To run this code just include it in your page, along with the core JQuery library. Any divs containing the class slideshow exist on the page then they will be converted into image swappers, as long as they contain images.

Comments

Permalink
Demo Please......

asddsfaf (Mon, 04/19/2010 - 14:18)

Add new comment

The content of this field is kept private and will not be shown publicly.