#StackBounty: #jquery #html #css #animation #dom jQuery Slideshow with pagination

Bounty: 50

The code below is for a slideshow that shows an .active slide, pre-loads a .next slide, and holds the .last slide in the background.

My big issue with this is there seems to be duplication and probably very inefficient code here that I would like to improve upon, but do not yet have that complete knowledge.

I am not looking for the code rewritten, but would like to hear of ideas that can improve not only my knwoledge of jQuery but also the effiency of the code.

<

div class=”snippet” data-lang=”js” data-hide=”false” data-console=”true” data-babel=”false”>

$(document).ready(function() {

  $('.slide:eq(-1)').addClass('last');
  $('.dot:first').addClass('active');
  $('.slide:first').addClass('active').delay($duration).queue(function() {
    $(this).addClass('show-text');
  });
  $('.slide:eq(1)').addClass('next');

});

// Globals
var $classes = 'last active show-text next';
var $duration = 1250;

// Function for the pagination operation
$('.dot').on('click', function() { // the dot click

  var $This = $(this);
  // Match the index to the slide numbers
  var GetIndex = $This.closest('li').index() + 1;

  $('.dot').removeClass('active').filter($This).addClass('active');

  $('.slide').dequeue();
  $('.slide').removeClass($classes);

  // Show new active slide
  $('#slide' + GetIndex).addClass('active').delay($duration).queue(function() {
    $(this).addClass('show-text');
  });
  // Add class to previous slide
  $('.slide').eq(($('.slide.active').index() - 1) % $('.slide').length).addClass('last');
  // Add class to next slide
  $('.slide').eq(($('.slide.active').index() + 1) % $('.slide').length).addClass('next');

});

$('.button').click(function moveSlide() {

  // Variables for moving slide
  var $active = $('.slide.active');
  var $prevSlide = $('.slide').eq(($active.index() - 1) % $('.slide').length);
  var $afterPrevSlide = $('.slide').eq(($active.index() - 2) % $('.slide').length);
  var $nextSlide = $('.slide').eq(($active.index() + 1) % $('.slide').length);
  var $slideAfterNext = $('.slide').eq(($active.index() + 2) % $('.slide').length);

  // Variables for pagination
  var $tagNextDot = $('#' + $prevSlide.attr('id') + 'Dot');
  var $tagPrevDot = $('#' + $nextSlide.attr('id') + 'Dot');

  $active.dequeue();
  $('.slide').removeClass($classes)
  $('.dot').removeClass('active');

  if ($(this).is("#prev")) {

    $active.addClass('next');
    $tagNextDot.addClass('active');
    $prevSlide.addClass('active').delay($duration).queue(function() {
      $(this).addClass('show-text');
    });
    $afterPrevSlide.addClass('last');

  } else {
    $active.addClass('last');
    $tagPrevDot.addClass('active');
    $nextSlide.addClass('active').delay($duration).queue(function() {
      $(this).addClass('show-text');
    });
    $slideAfterNext.addClass('next');
  }

});
body {
  font-size: 16px;
  font-family: 'Heebo', sans-serif;
  text-transform: uppercase;
  font-weight: 900;
}


/* Slides */

.slide-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  display: flex;
  overflow: hidden;
}

.slide {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 70%;
  left: 140%;
  z-index: 0;
  transition: 1.25s;
  box-shadow: -10px 0px 21px -5px rgba(0, 0, 0, 0.5);
}

.slide h2 {
  display: none;
  color: #fff;
  text-shadow: 0px 0px 8px rgba(0, 0, 0, 0.5);
  letter-spacing: -2px;
  font-size: 3rem;
}

.slide.active.show-text h2 {
  display: block;
  animation: reveal-text 1.5s forwards;
}

@keyframes reveal-text {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

#slide1 {
  background: linear-gradient(to right, #ff416c, #ff4b2b);
}

#slide2 {
  background: linear-gradient(to right, #00b4db, #0083b0);
}

#slide3 {
  background: linear-gradient(to right, #59c173, #a17fe0, #5d26c1);
}

#slide4 {
  background: linear-gradient(to right, #ad5389, #3c1053);
}

.slide.last {
  left: 0;
  z-index: 0;
}

.slide.active {
  left: 0;
  z-index: 1;
}

.slide.next {
  left: 70%;
  z-index: 2;
}

.dots-wrapper {
  z-index: 10;
  list-style: none;
  padding-left: 0;
  position: absolute;
  bottom: 0;
  padding: 10px;
}

.dots-wrapper li {
  display: inline;
}

.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border: 2px solid #fff;
  border-radius: 6px;
}

.dot.active {
  background-color: red;
  border-color: red;
}


/* Buttons */

.button-wrapper {
  display: flex;
  z-index: 10;
  width: 100%;
  justify-content: space-between;
  align-items: center;
}

.button {
  background-color: rgba(0, 0, 0, 0.45);
  color: #ddd;
  height: 40px;
  border: none;
  font-weight: bold;
  padding: 10px 20px;
  transition: 0.3s;
}

.button:hover {
  cursor: pointer;
  background: rgba(0, 0, 0, 0.85);
  color: #fff;
}
https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js

Slide One.

Slide Two.

Slide Three.

Slide Four.

<ul class="dots-wrapper"> <li> <span id="slide1Dot" class="dot"></span> </li> <li> <span id="slide2Dot" class="dot"></span> </li> <li> <span id="slide3Dot" class="dot"></span> </li> <li> <span id="slide4Dot" class="dot"></span> </li> </ul> </div>


Get this bounty!!!