JSFiddle - React, Tailwind, and code Playground

https://teratail.com/questions/307543

by Lhankor_Mhy

HTML

<div id="container">
  <div class="wrap">
      <div class="img"></div>
      <div class="img"></div>
      <div class="img"></div>
      <div class="img"></div>
    <div class="img"></div>
  </div>

  <div class="wrap">
      <div class="img"></div>
      <div class="img"></div>
      <div class="img"></div>
      <div class="img"></div>
      <div class="img"></div>
  </div>
</div>

CSS

#container {
  display: flex;
}

.wrap {
 width: 50%;
  position: relative;
}

.img {
  width: 200px;
  height: 200px;
  background-color: #000;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  overflow: hidden;
}

.img.active {
          z-index: 3 !important;
          animation: slideNext 1s linear 0s forwards;
        }

.img.out {
  z-index: 2;
}

@keyframes slideNext {
  0% {
    -webkit-clip-path: polygon(0 0, 0 0, 0 100%, 0 100%);
    clip-path: polygon(0 0, 0 0, 0 100%, 0 100%);
  }
  100% {
      -webkit-clip-path: polygon(100% 0, 0 0, 0 100%, 100% 100%);
      clip-path: polygon(100% 0, 0 0, 0 100%, 100% 100%);
  }
}

.img:nth-of-type(1) {
  background-color: #333;
}

.img:nth-of-type(2) {
  background-color: #444;
}

.img:nth-of-type(3) {
  background-color: #555;
}

.img:nth-of-type(4) {
  background-color: #666;
}

.img:nth-of-type(5) {
  background-color: #777;
}

JavaScript

$(window).on('load', function () {
  $('.wrap').each(function(i,e){
    
    $('.img:first', e).addClass('active out');
  
    var fade_image01 = $('.img', e);
    var fade_time = 4000;
  
    if (fade_image01.length) {
  
      var current = 0;
      var old = 0;
      var next = 1;
  
      setInterval(function () {
  
        old = current;
        current = current === fade_image01.length - 1 ? 0 : current + 1;
        //console.log(current);
        next = next === fade_image01.length - 1 ? 0 : next + 1;
  
  
        fade_image01.eq(current).addClass('active out');
        fade_image01.eq(old).removeClass('active');
  
        setTimeout(function () {
          fade_image01.eq(old).removeClass('out');
        }, 2000);
  
      }, fade_time);
    }
  })

});