原生js实现幻灯片效果

<div class="carousel-container">  
        <div class="carousel">  
            <div class="carousel-item active">Item 1</div>  
            <div class="carousel-item">Item 2</div>  
            <div class="carousel-item">Item 3</div>  
        </div>  
        <button class="carousel-control prev">Prev</button>  
        <button class="carousel-control next">Next</button>  
        <div class="carousel-indicators">  
            <span class="indicator active" data-index="0"></span>  
            <span class="indicator" data-index="1"></span>  
            <span class="indicator" data-index="2"></span>  
        </div>  
</div>  
body {  
    font-family: Arial, sans-serif;  
    display: flex;  
    justify-content: center;  
    align-items: center;  
    height: 100vh;  
    margin: 0;  
    background-color: #f0f0f0;  
}  
  
.carousel-container {  
    position: relative;  
    width: 80%;  
    max-width: 600px;  
    overflow: hidden;  
    border: 2px solid #ccc;  
    border-radius: 10px;  
    background-color: #fff;  
}  
  
.carousel {  
    display: flex;  
    transition: transform 0.5s ease-in-out;  
}  
  
.carousel-item {  
    min-width: 100%;  
    box-sizing: border-box;  
    text-align: center;  
    padding: 20px;  
    border-right: 1px solid #ccc;  
}  
  
.carousel-item:last-child {  
    border-right: none;  
}  
  
.carousel-control {  
    position: absolute;  
    top: 50%;  
    transform: translateY(-50%);  
    background-color: rgba(0, 0, 0, 0.5);  
    color: #fff;  
    border: none;  
    padding: 10px;  
    cursor: pointer;  
    z-index: 10;  
}  
  
.carousel-control.prev {  
    left: 10px;  
}  
  
.carousel-control.next {  
    right: 10px;  
}  
  
.carousel-indicators {  
    display: flex;  
    justify-content: center;  
    margin-top: 10px;  
}  
  
.indicator {  
    width: 10px;  
    height: 10px;  
    background-color: #ccc;  
    border-radius: 50%;  
    margin: 0 5px;  
    cursor: pointer;  
}  
  
.indicator.active {  
    background-color: #333;  
}
document.addEventListener('DOMContentLoaded', () => {  
    const carousel = document.querySelector('.carousel');  
    const items = document.querySelectorAll('.carousel-item');  
    const indicators = document.querySelectorAll('.indicator');  
    const prevButton = document.querySelector('.carousel-control.prev');  
    const nextButton = document.querySelector('.carousel-control.next');  
    let currentIndex = 0;  
  
    function updateCarousel() {  
        items.forEach((item, index) => {  
            item.classList.toggle('active', index === currentIndex);  
        });  
  
        indicators.forEach((indicator, index) => {  
            indicator.classList.toggle('active', index === currentIndex);  
        });  
  
        carousel.style.transform = `translateX(${-currentIndex * 100}%)`;  
    }  
  
    function showNextItem() {  
        currentIndex = (currentIndex + 1) % items.length;  
        updateCarousel();  
    }  
  
    function showPrevItem() {  
        currentIndex = (currentIndex - 1 + items.length) % items.length;  
        updateCarousel();  
    }  
  
    prevButton.addEventListener('click', () => {  
        showPrevItem();  
    });  
  
    nextButton.addEventListener('click', () => {   
        showNextItem();  
    });  
  
    indicators.forEach(indicator => {  
        indicator.addEventListener('click', (event) => {  
            const targetIndex = parseInt(event.target.getAttribute('data-index'));  
            currentIndex = targetIndex;  
            updateCarousel();   
        });  
    });  
});

 

posted @ 2024-10-23 17:56  eternityQSL  阅读(17)  评论(0编辑  收藏  举报