解释起来有点困难,所以我制作了这个 codepen供参考。我正在尝试使用 vanilla JavaScript 克隆 Bootstrap 轮播,我在幻灯片之间添加了过渡,这对于第一张和最后一张幻灯片之间存在的所有幻灯片都很有效,但是当我单击上一个按钮从第一张幻灯片转到最后一张幻灯片或单击下一个按钮从从最后一张幻灯片到第一张幻灯片,这种过渡行为很奇怪,它沿着中间的所有其他幻灯片移动。
这是我使用的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>
和 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)`;
});
请帮我解决这个轮播的这些结束幻灯片之间的过渡问题?谢谢
慕村225694
相关分类