简体   繁体   English

如何在轮播的第一张和最后一张幻灯片之间添加平滑过渡?

[英]How to add smooth transition between first and last slide of a carousel?

It is a bit hard to explain so I made this codepen for any reference.这有点难以解释,所以我制作了这个代码笔以供参考。 I am trying to clone Bootstrap carousel using vanilla JavaScript, I added transition between slides which works nice for all slides exists between first and last slide, but as I click previous button to go from first slide to last slide or clicking next button to go from last slide to first slide this transition behaves weird it travels along all other slides in between.我正在尝试使用 vanilla JavaScript 克隆 Bootstrap carousel,我添加了幻灯片之间的过渡,这对于第一张和最后一张幻灯片之间存在的所有幻灯片都很好,但是当我单击上一个按钮从第一张幻灯片转到最后一张幻灯片或单击下一个按钮时从最后一张幻灯片到第一张幻灯片,这个过渡表现得很奇怪,它沿着中间的所有其他幻灯片移动。

This is the html I use:这是我使用的html:

   <div class="carousel-container">
    <div class="carousel-slide">
      <img src="imgs/pc1(1).jpg" class="firstSlide" alt="">
      <img src="imgs/pc1(2).jpg" alt="">
      <img src="imgs/pc1(3).jpg" class="lastSlide" alt="">
    </div>
  </div>
  <button id="prevBtn">Prev</button>
  <button id="nextBtn">Next</button>

and the JavaScript:和 JavaScript:

const carousel_slide = document.querySelector(".carousel-slide");
const carousel_images = document.querySelectorAll(".carousel-slide img");
const prev_btn = document.querySelector("#prevBtn");
const next_btn = document.querySelector("#nextBtn");

let counter = 0;
let size = carousel_images[0].clientWidth;

next_btn.addEventListener("click", () => {
  carousel_slide.style.transition = "all 0.3s ease-in-out";
  counter++;
  if (counter >= carousel_images.length) {
    counter = -1;
    counter++;
  }
  carousel_slide.style.transform = `translateX(${-size * counter}px)`;
});

prev_btn.addEventListener("click", () => {
  carousel_slide.style.transition = "all 0.3s ease-in-out";
  counter--;
  if (counter < 0) {
    counter = carousel_images.length - 2;
    counter++;
  }
  carousel_slide.style.transform = `translateX(${-size * counter}px)`;
});

Please help me solve this transition problem between these ending slides of this carousel?请帮我解决这个轮播的这些结束幻灯片之间的过渡问题? Thank you谢谢

First you duplicate the first slide and insert in the end, and duplicate the last slide and insert in the beginning:首先复制第一张幻灯片并插入到最后,然后复制最后一张幻灯片并插入到开头:

var lastSlide = carousel_images[slidecount - 1].outerHTML;
var firstSlide = carousel_images[0].outerHTML;
carousel_slide.insertAdjacentHTML('afterbegin', lastSlide);
carousel_slide.insertAdjacentHTML('beforeend', firstSlide);
// Shift to the second slide:
carousel_slide.style.transform = `translateX(${-size}px)`;

When you move to the slide before the first (a copy of the last), you wait for the animation to end, and silently shift to the last:当你移动到第一张之前的幻灯片(最后一张的副本)时,你等待动画结束,然后默默地移到最后一张:

if (counter < 0) {
  setTimeout(() => {
    counter = carousel_images.length - 1;
    carousel_slide.style.transition = "none";
    carousel_slide.style.transform = `translateX(${-size * (counter + 1)}px)`;
  }, 300);
}

And when you move to the slide after the last (a copy of the first), you silently shift to the first:当你移到最后一张(第一张的副本)之后的幻灯片时,你会默默地移到第一张:

if (counter >= carousel_images.length) {
  setTimeout(() => {
    counter = 0;
    carousel_slide.style.transition = "none";
    carousel_slide.style.transform = `translateX(${-size}px)`;
  }, 300);
}

Working demo: https://codepen.io/bortao/pen/MWwOVbd工作演示: https : //codepen.io/bortao/pen/MWwOVbd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM