Posted By

1man on 03/06/09


Tagged

image simple jquery fader


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

jfherring
acristic
vali29
cfleschhut


Simple jQuery Imagefader


 / Published in: jQuery
 

Simple imagefader to rotate around a few images. Seem to be doing this a lot in various projects so decided to put something a bit more generic together. Uses jQuery. Images named as car1.jpg, car2.jpg, car3..... etc.

  1. /**
  2.  * Image Cycle
  3.  */
  4. //Settings
  5. var faderSettings = {
  6. timing: 5000,
  7. fadeSpeed: 400,
  8. numberOfImages: 4,
  9. imagePrefix: "car",
  10. imageSuffix: ".jpg",
  11. imageDirectory: "../images/"
  12. };
  13. var displayImage = function(displayImage){
  14. var imageURL = faderSettings.imageDirectory + displayImage;
  15. $("#imageContainer").fadeOut(faderSettings.fadeSpeed, function(){
  16. $(this).css({
  17. 'backgroundImage': 'url('+ imageURL + ')'
  18. }).fadeIn(faderSettings.fadeSpeed);
  19. });
  20. };
  21. function outer(){
  22. var a = 1;
  23. function inner(){
  24. if(a==faderSettings.numberOfImages){
  25. a = 1;
  26. } else {a++;}
  27. var imageNeeded = faderSettings.imagePrefix + a + faderSettings.imageSuffix;
  28. displayImage(imageNeeded);
  29. }
  30. return inner;
  31. }
  32. var imageFade = outer();
  33. var cycleMe = setInterval(imageFade, faderSettings.timing);

Report this snippet  

You need to login to post a comment.