$('.grid__sliderShow').each(function() { var $this = $(this); var $group = $this.find('.grid__slideGroup'); var $slides = $this.find('.grid__slide300'); var bulletArray = []; var currentIndex = 0; var timeout; function move(newIndex) { var animateLeft, slideLeft; advance(); if ($group.is(':animated') || currentIndex === newIndex) { return; } bulletArray[currentIndex].removeClass('active'); bulletArray[newIndex].addClass('active'); if (newIndex > currentIndex) { slideLeft = '100%'; animateLeft = '-100%'; } else { slideLeft = '-100%'; animateLeft = '100%'; } $slides.eq(newIndex).css({ display: 'block', left: slideLeft }); $group.animate({ left: animateLeft }, function() { $slides.eq(currentIndex).css({ display: 'none' }); $slides.eq(newIndex).css({ left: 0 }); $group.css({ left: 0 }); currentIndex = newIndex; }); } function advance() { clearTimeout(timeout); timeout = setTimeout(function() { if (currentIndex < ($slides.length - 1)) { move(currentIndex + 1); } else { move(0); } }, 4000); } $.each($slides, function(index) { var $button = $('" '); if (index === currentIndex) { $button.addClass('active'); } $button.on('click', function() { move(index); }).appendTo('.grid__slideBottons'); bulletArray.push($button); }); advance(); });