Prevent event overlap in custom jQuery image carousel

Prevent event overlap in custom jQuery image carousel

I am working on a custom image carousel, using jQuery and CSS. My aim is to make it really  lightweight   but with (just) enough features: "bullets", auto-advance, responsiveness.


The slider had a bug (described in detail HERE): when I clicked 2 bullets in rapid succession, the transition would overlap, instead of "standing in line". The fix I found is making all the bullets except the active one non-responsive to clicks,  instead of queueing the clicks :


  1. var $elm = $('.slider'),
  2.     $slidesContainer = $elm.find('.slider-container'),
  3.     slides = $slidesContainer.children('a'),
  4.     slidesCount = slides.length,
  5.     slideHeight = $(slides[0]).find('img').outerHeight(false),
  6.     animationspeed = 1500,
  7.     animationInterval = 7000;

  8. // Set (initial) z-index for each slide
  9. var setZindex = function() {
  10.     for (var i = 0; i < slidesCount; i++) {
  11.         $(slides[i]).css('z-index', slidesCount - i);
  12.     }
  13. };
  14. setZindex();

  15. var displayImageBeforeClick = null;

  16. var setActiveSlide = function() {
  17.     $(slides).removeClass('active');
  18.     $(slides[activeIdx]).addClass('active');
  19. };

  20. var advanceFunc = function() {
  21.     if ($('.slider-nav li.activeSlide').index() + 1 != $('.slider-nav li').length) {
  22.         $('.slider-nav li.activeSlide').next().find('a').trigger('click');
  23.     } else {
  24.         $('.slider-nav li:first').find('a').trigger('click');
  25.     }
  26. }

  27. var autoAdvance = setInterval(advanceFunc, animationInterval);

  28. //Set slide height
  29. $(slides).css('height', slideHeight);

  30. // Append bullets
  31. if (slidesCount > 1) {
  32.   /* Prepend the slider navigation to the slider
  33.      if there are at least 2 slides */
  34.   $elm.prepend('<ul class="slider-nav"></ul>');
  35.   
  36.   // make a bullet for each slide
  37.   for (var i = 0; i < slidesCount; i++) {
  38.     var bullets = '<li><a href="#">' + i + '</a></li>';
  39.     if (i == 0) {
  40.       // active bullet
  41.       var bullets = '<li class="activeSlide"><a href="#">' + i + '</a></li>';
  42.       // active slide
  43.       $(slides[0]).addClass('active');
  44.     }
  45.     $('.slider-nav').append(bullets);
  46.   }
  47. };

  48. var animationStart = false;
  49. var slideUpDown = function() {
  50.     animationStart = true;
  51.     // set top property for all the slides
  52.     $(slides).not(displayImageBeforeClick).css('top', slideHeight);
  53.     // then animate to the next slide
  54.     $(slides[activeIdx]).animate({
  55.         'top': 0
  56.     }, animationspeed, function() {
  57.         animationStart = false;
  58.     });

  59.     $(displayImageBeforeClick).animate({
  60.         'top': "-100%"
  61.     }, animationspeed, function() {
  62.         animationStart = false;
  63.     });
  64. };

  65. $('.slider-nav a').on('click', function(event) {
  66.     if (animationStart) {
  67.         return false;
  68.     }
  69.     displayImageBeforeClick = $(".slider-container .active");
  70.     activeIdx = $(this).text();
  71.     if ($(slides[activeIdx]).hasClass("active")) {
  72.         return false;
  73.     }
  74.     $('.slider-nav a').closest('li').removeClass('activeSlide');
  75.     $(this).closest('li').addClass('activeSlide');
  76.     // Reset autoadvance if user clicks bullet
  77.     if (event.originalEvent !== undefined) {
  78.         clearInterval(autoAdvance);
  79.         autoAdvance = setInterval(advanceFunc, animationInterval);
  80.     }

  81.     setActiveSlide();
  82.     slideUpDown();
  83. });


I have posted a jsFuddle with all the code HERE .


Questions: How can I queue the clicks? Are there any better alternatives to queueing the clicks?