原生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 @   eternityQSL  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了

喜欢请打赏

扫描二维码打赏

支付宝打赏

点击右上角即可分享
微信分享提示