跳至内容
const carouselSlide = document.querySelector('.carousel-slide');
const items = document.querySelectorAll('.carousel-item');
const prevButton = document.querySelector('.carousel-prev');
const nextButton = document.querySelector('.carousel-next');
让 index = 0;
function showItem(index) {
carouselSlide.style.transform = `translateX(${-index * 100}%)`;
}
nextButton.addEventListener('click', () => {
index = (index + 1) % items.length;
showItem(index);
});
prevButton.addEventListener('click', () => {
index = (index - 1 + items.length) % items.length;
显示项目(索引);
});