← 목록으로 돌아가기

⏰ 02. 자동재생 슬라이더

💡 특징: 3초마다 자동으로 슬라이드가 넘어갑니다. 사용자가 조작하면 자동재생이 멈춥니다.
🌅 아침
🌞 낮
🌆 저녁
🌙 밤
⭐ 새벽

📝 구현 코드

JavaScript

var swiper = new Swiper(".mySwiper", {
    slidesPerView: 1,
    spaceBetween: 30,
    loop: true,
    autoplay: {
        delay: 3000,                    // 3초마다 넘어감
        disableOnInteraction: false,    // 사용자 조작 후에도 자동재생 유지
    },
    pagination: {
        el: ".swiper-pagination",
        clickable: true,
    },
    navigation: {
        nextEl: ".swiper-button-next",
        prevEl: ".swiper-button-prev",
    },
});

// 자동재생 제어 함수들
function toggleAutoplay() {
    if (swiper.autoplay.running) {
        swiper.autoplay.stop();
    } else {
        swiper.autoplay.start();
    }
}

function restartAutoplay() {
    swiper.autoplay.start();
}