CSS/JS Slider Animation Loop with Sequential Slide Positions

vc9ivgsu  于 2023-08-09  发布在  其他
关注(0)|答案(1)|浏览(99)

我想实现一个CSS/JS滑块,应该做以下事情
1.应该有2个或更多div
1.它应该在加载时显示第一个div 3秒,然后第一个div应该被第二个div替换,第二个div替换为第三个div,依此类推。
1.动画必须在一个序列(右,下,左,上…)
1.动画应该循环播放
1.每个div必须显示3秒钟,然后显示下一张幻灯片
我尝试了以下代码:https://codesandbox.io/s/nostalgic-dewdney-lnz8kr?file=/index.html
我有一些问题的代码,如,它不播放循环,并有一个不同的持续时间的每一张幻灯片,我在哪里出错了?

fcipmucu

fcipmucu1#

我会改变班级。大概是这样的:

document.querySelectorAll('.slider-container').forEach(slider => {
  const delay = Number(slider.dataset.delay) || 0;
  const transitionDuration = Number(slider.dataset.transitionDuration) || 400;
  let autoplayDelay = Number(slider.dataset.autoplayDelay) || 3000;
  if (autoplayDelay - transitionDuration < 100) {
    autoplayDelay = transitionDuration + 100;
  }
  
  const classes = ['from-right', 'from-bottom', 'from-left', 'from-top'];
  let nextClassIndex = -1;
  
  setTimeout(() => {
    setTimeout(() => {
      slide();
      setInterval(slide, autoplayDelay);
     }, autoplayDelay)
  }, delay);
  
  function slide() {
    const activeSlide = slider.querySelector('.slide.active');
    const nextSlide = activeSlide.nextElementSibling ?? slider.querySelector('.slide:first-child');
    const animationClass = classes[nextClassIndex + 1];
    nextClassIndex++;
    if (nextClassIndex === classes.length - 1) {
      nextClassIndex = -1;
    }
    
    classes.map(className => activeSlide.classList.remove(className));
    activeSlide.style.removeProperty('transition-duration');
    activeSlide.style.removeProperty('z-index');
    
    nextSlide.classList.add(animationClass);
    nextSlide.style.transitionDuration = transitionDuration + 'ms';
    nextSlide.style.zIndex = 6;
    nextSlide.classList.add('active');
     
    setTimeout(() => nextSlide.classList.add('animated'), 100)
    setTimeout(() => activeSlide.classList.remove('active', 'animated'), transitionDuration + 100 );
  }
})
.slider-container {
  position: relative;
  width: 200px;
  height: 200px;
  overflow: hidden;
}

.slide {
  position: absolute;
  inset:0;
  transition-property: transform;
  will-change: transform;
  display:none;
}

.slide.from-right {
  transform: translateX(100%);
}

.slide.from-left {
  transform: translateX(-100%);
}

.slide.from-top {
  transform: translateY(-100%);
}

.slide.from-bottom {
  transform: translateY(100%);
}

.slide.animated {
  transform: none;
}

.slide.active {
  display: block
}
<div class="slider-container">
  <div class="slide active" style="background-color: red;">0</div>
  <div class="slide" style="background-color: blue;">1</div>
 </div>
<div
  class="slider-container"
  data-delay="2000"
  data-transition-duration="800"
>
  <div class="slide active" style="background-color: red;">0</div>
  <div class="slide" style="background-color: blue;">1</div>
  <div class="slide" style="background-color: green;">2</div>
  <div class="slide" style="background-color: yellow;">3</div>
  <div class="slide" style="background-color: brown;">4</div>
  <div class="slide" style="background-color: red;">5</div>
  <div class="slide" style="background-color: blue;">6</div>
  <div class="slide" style="background-color: green;">7</div>
 </div>

相关问题