jQuery simple slideshow


SUBMITTED BY: Guest

DATE: Nov. 21, 2013, 7:07 p.m.

FORMAT: JavaScript

SIZE: 3.5 kB

HITS: 1468

  1. /* usage example
  2. $('#elementID').viewSlide(
  3. '.element-childclass',
  4. clickTransitionSpeed, // ms
  5. autoSlide, // boolean
  6. slideSpeed, // ms
  7. autoTransitionSpeed, // ms
  8. initialDelay // ms
  9. );
  10. */
  11. $.fn.viewSlide = function() {
  12. var args = arguments[0] || {};
  13. var slideContainer = $(this);
  14. $(this).append('<div class="slideshow-arrow slideshow-arrow-left"></div>');
  15. $(this).append('<div class="slideshow-arrow slideshow-arrow-right"></div>');
  16. var element = arguments[0];
  17. var speed = arguments[1];
  18. var autoSlide = arguments[2];
  19. var autoSlideInterval = arguments[3];
  20. var autoSlideTransitionSpeed = arguments[4];
  21. var initialDelay = arguments[5];
  22. // elements in array
  23. var slides = 0;
  24. $(this).find(element).each(function() {
  25. slides++;
  26. $(this).attr("sn", slides);
  27. $(this).addClass('slide-' + slides);
  28. });
  29. // hide all elements
  30. $(this).find(element).hide();
  31. // show first element
  32. $(this).find(element).eq(0).show();
  33. $(this).find(element).eq(0).addClass('active-slide');
  34. // set defaults
  35. var activeSlide = 1;
  36. var nextSlide;
  37. var mouseOverContainer = false;
  38. /* next slide */
  39. function slideNext(transitionSpeed) {
  40. // jump to animation end
  41. slideContainer.stop(true,true);
  42. activeSlide = parseInt(slideContainer.find('.active-slide').attr('sn'));
  43. nextSlide = activeSlide + 1;
  44. if(activeSlide == slides) {
  45. nextSlide = 1;
  46. }
  47. slideContainer.find(element).fadeOut(transitionSpeed);
  48. slideContainer.find(element).removeClass('active-slide');
  49. slideContainer.find('.slide-' + nextSlide).addClass('active-slide');
  50. slideContainer.find('.slide-' + nextSlide).fadeIn(transitionSpeed);
  51. }
  52. function slidePrev(transitionSpeed) {
  53. // jump to animation end
  54. slideContainer.stop(true,true);
  55. activeSlide = parseInt(slideContainer.find('.active-slide').attr('sn'));
  56. nextSlide = activeSlide - 1;
  57. if(activeSlide == 1) {
  58. nextSlide = slides;
  59. }
  60. slideContainer.find(element).fadeOut(transitionSpeed);
  61. slideContainer.find(element).removeClass('active-slide');
  62. slideContainer.find('.slide-' + nextSlide).addClass('active-slide');
  63. slideContainer.find('.slide-' + nextSlide).fadeIn(transitionSpeed);
  64. }
  65. $(this).find('.slideshow-arrow-right').click(function(){
  66. slideNext(speed);
  67. });
  68. /* prev slide */
  69. $(this).find('.slideshow-arrow-left').click(function(){
  70. slidePrev(speed);
  71. });
  72. // automatic sliding
  73. if(autoSlide) {
  74. if(initialDelay) {
  75. setTimeout(function(){
  76. var slideTimer = setInterval(function() {
  77. if(!mouseOverContainer) {
  78. slideNext(autoSlideTransitionSpeed);
  79. }
  80. }, autoSlideInterval);
  81. },initialDelay);
  82. } else {
  83. var slideTimer = setInterval(function() {
  84. if(!mouseOverContainer) {
  85. slideNext(autoSlideTransitionSpeed);
  86. }
  87. }, autoSlideInterval);
  88. }
  89. }
  90. // autoSlide on off mouseenter/leave
  91. slideContainer.mouseenter(function(){
  92. mouseOverContainer = true;
  93. });
  94. slideContainer.mouseleave(function(){
  95. mouseOverContainer = false;
  96. });
  97. }

comments powered by Disqus