无缝轮播图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width='device-width', initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="box">
        <div class="carousel">
            <div class="slide" id="slide">
                <img src="1.jpeg" style="width: 100%;height:250px" alt="">
                <img src="2.jpeg" style="width: 100%;height:250px" alt="">
                <img src="3.jpeg" style="width: 100%;height:250px" alt="">
            </div>
            <div class="caditor" style="z-index: 999;">
                <li class="li-active"></li>
                <li></li>
                <li></li>
            </div>
            <div class="btn-group">
                <button class="left" ><</button>
                <button class="right" >></button>
            </div>
        </div>
    </div>
    <script>
        window.onload=()=>{
            const timerInter = 4000;
            let currentIndex = 1
            var timer = null;
            let slide = document.querySelector('.slide');
            let firstelement = slide.firstElementChild.cloneNode(true);
            let lastelement = slide.lastElementChild.cloneNode(true);
            let Nodelength = slide.children.length+2;
            let caditor = document.querySelector('.caditor')
            let caditorList = caditor.children;
            var width = window.innerWidth
            var leftbtn=document.querySelector('.left')
            var rightbtn=document.querySelector('.right')
            slide.appendChild(firstelement)
            slide.insertBefore(lastelement,slide.firstChild)
            slide.style.left = `-${width}px`
            slide.style.transition = 'left 1.5s';
            for(let i=0;i<caditorList.length;i++)
            {
                caditorList[i].onclick=function caditorclick()
                {
                    if(i==currentIndex-1)return;
                    clearInterval(timer)
                    currentIndex=i+1;
                    playindicator(currentIndex-1)
                    play(currentIndex)
                    let timeout = setTimeout(()=>{
                        return autoplay()
                    },0)
                }
            }
            autoplay()
            leftbtn.onclick=()=>{
                btnclick('left')
            }
            rightbtn.onclick=()=>{
                btnclick('right')
            }
            function btnclick(e)
            {
                clearInterval(timer);
                if(e=='left')
                {
                    currentIndex--
                    play(currentIndex);
                    if(currentIndex>=1)
                    {
                        playindicator(currentIndex-1)
                        clearInterval(timer)
                        autoplay()
                    }
                    if(currentIndex==0)
                    {
                        currentIndex=Nodelength-2;
                        playindicator(currentIndex-1)
                        rightbtn.disabled=true;
                        leftbtn.disabled=true;
                        setTimeout(() => {
                            rightbtn.disabled=false;
                            leftbtn.disabled=false;
                            slide.style.left = `-${width*(currentIndex)}px`
                            slide.style.transition = 'left 0s';
                            autoplay()
                        }, timerInter);
                    }
                    // if(currentIndex>=1)
                    // {
                    //     playindicator(currentIndex-1)
                    //    return setTimeout(() => {
                    //         return autoplay()
                    //     }, timerInter);
                    // }
                    // if(currentIndex==0)
                    // {
                    //     leftbtn.disabled=true;
                    //     rightbtn.disabled=true;
                    //     currentIndex=3;
                    //     caditor.style.zIndex=0;
                    //     playindicator(currentIndex-1)
                    //     setTimeout(()=>{
                    //         caditor.style.zIndex=999;
                    //         leftbtn.disabled=false;
                    //         rightbtn.disabled=false;
                    //         slide.style.left = `-${width*(currentIndex)}px`
                    //         slide.style.transition = 'left 0s';
                    //         return autoplay()
                    //     },timerInter)
                    // }
                    // 上一页
                }else
                {
                    // 下一页
                    currentIndex++;
                    play(currentIndex);
                    if(currentIndex<=3)
                    {
                        playindicator(currentIndex-1)
                        clearInterval(timer)
                        autoplay()
                    }
                    if(currentIndex==Nodelength-1)
                    {
                        rightbtn.disabled=true;
                        leftbtn.disabled=true;
                        currentIndex=1;
                        playindicator(currentIndex-1)
                        setTimeout(() => {
                            slide.style.left = `-${width*(currentIndex)}px`
                            slide.style.transition = 'left 0s';
                            rightbtn.disabled=false;
                            caditor.style.zIndex=999;
                            leftbtn.disabled=false;
                            autoplay()
                        }, timerInter);
                    }
                    // if(currentIndex<=3)
                    // {
                    //     playindicator(currentIndex-1)
                    //     setTimeout(() => {
                    //         return autoplay()
                    //     }, timerInter);
                    // }
                    // if(currentIndex==Nodelength-1)
                    // {
                    //     rightbtn.disabled=true;
                    //     leftbtn.disabled=true;
                    //     currentIndex=1;
                    //     caditor.style.zIndex=0;
                    //     playindicator(currentIndex-1)
                    //     let timeout = setTimeout(()=>{
                    //         rightbtn.disabled=false;
                    //         caditor.style.zIndex=999;
                    //         leftbtn.disabled=false;
                    //         slide.style.left = `-${width*(currentIndex)}px`
                    //         slide.style.transition = 'left 0s';
                    //          autoplay()
                    //     },timerInter)
                    // }
                }
            }
            function autoplay()
            {
                timer = setInterval(() => {
                    currentIndex++;
                    if(currentIndex<Nodelength)
                    {
                        play(currentIndex);
                        if(currentIndex<Nodelength-1)
                        {
                            playindicator(currentIndex-1)
                        }else if(currentIndex>currentIndex-2)
                        {
                            playindicator(0)
                        }
                        if(currentIndex==Nodelength-1)
                        {
                            caditor.style.zIndex=0;
                            rightbtn.disabled=true;
                            leftbtn.disabled=true;
                            setTimeout(() => {
                                clearInterval(timer)
                                caditor.style.zIndex=999;
                                rightbtn.disabled=false;
                                leftbtn.disabled=false;
                                slide.style.left = `-${width}px`
                                slide.style.transition = 'left 0s';
                                currentIndex = 1;
                                playindicator(currentIndex-1)
                                return autoplay()
                            }, timerInter);
                        }
                    }
                }, timerInter);
            }
            function play(index) {
                slide.style.left = `-${width*index}px`
                slide.style.transition = 'left 1.5s';
            }
            function playindicator(index) {
                for(let i=0; i<caditorList.length;i++)
                {
                    caditorList[i].classList.remove('li-active')
                }
                caditorList[index].classList.add('li-active')
              }
        }
        
    </script>
</body>
<style>
    html,body{
        width:100%;
        height: 100%;
        margin: 0;
        padding: 0;
    }
    .carousel
    {
        position: relative;
        width: 100%;
        height: 250px;
    }
    .box{
        width: 100%;
        background-color: #ccc;
        height: 250px;
        overflow: hidden;
    }
    .slide{
        width: 100%;
        height: 250px;
        display: flex;
        position: absolute;
    }
    .slide img{
        flex-shrink: 0;
        width: 100%;
        height: 250px;
    }
    .caditor{
        position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    }
    .li-active{
        background-color: red;
        width: 50px;
        height: 10px;
        margin: 0 10px;
    }
    li{
        list-style: none;
        width: 50px;
        height: 10px;
        background-color: #ccc;
        margin: 0 10px;
        cursor: pointer;
    }
    .btn-group
    {
        position: relative;
        width: 100%;
        height: 250px;
    }
    .left{
        position: absolute;
        left: 10px;
        top: 50%;
        cursor: pointer;
        font-size:40px
    }
    .right{
        position: absolute;
        cursor: pointer;
        right: 10px;
        top: 50%;
        font-size:40px
    }
</style>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值