22 lines
534 B
JavaScript
22 lines
534 B
JavaScript
let currentIndex = 0;
|
|
|
|
function changeSlide(direction) {
|
|
const images = document.querySelector('.carousel-images');
|
|
const totalImages = images.querySelectorAll('img').length;
|
|
currentIndex += direction;
|
|
|
|
if (currentIndex < 0) {
|
|
currentIndex = totalImages - 1;
|
|
} else if (currentIndex >= totalImages) {
|
|
currentIndex = 0;
|
|
}
|
|
|
|
const translateX = -currentIndex * 100;
|
|
images.style.transform = `translateX(${translateX}%)`;
|
|
}
|
|
|
|
// 自动轮播
|
|
setInterval(() => {
|
|
changeSlide(1);
|
|
}, 3000);
|