用jQuery来写轮播图

轮播图

<style>
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }
    body {
        background-color: #cccccc;
    }
    .container {
        width: 500px;
        height: 300px;
        margin: 50px auto;
        position: relative;
    }
    .wipper {
        width: 300px;
    }
    .wipper li {
        width: 500px;
        height: 300px;
        display: none;
    }
    .wipper li:first-of-type {
        background-color: red;
    }
    .wipper li:nth-of-type(2) {
        background-color: orange;
    }
    .wipper li:nth-of-type(3) {
        background-color: yellow;
    }
    .wipper li:nth-of-type(4) {
        background-color: green;
    }
    .wipper li:nth-of-type(5) {
        background-color: cyan;
    }
    .wipper li:nth-of-type(6) {
        background-color: blue;
    }
    .wipper li:nth-of-type(7) {
        background-color: purple;
    }
    .wipper li:first-of-type {
        display: block;
    }
    .round {
        position: absolute;
        bottom: 30px;
        right: 50px;
        width: 200px;
        height: 30px;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
    }
    .round li {
        width: 10px;
        height: 10px;
        border-radius: 50%;
        background-color: white;
        margin: 0px 7px;
    }
    .round li.color {
        background-color: black;
    }
    .arrowleft {
        width: 15px;
        height: 50px;
        background-color: black;
        position: absolute;
        top: 125px;
        left: -15px;
        cursor: pointer;
    }
    .arrowright {
        width: 15px;
        height: 50px;
        background-color: black;
        position: absolute;
        top: 125px;
        right: -15px;
        cursor: pointer;
    }
</style>
<body>

    <div class="container">
        <ul class="wipper">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
        <ul class="round">

            <li class="color"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>


        </ul>

        <div class="arrowleft">

        </div>

        <div class="arrowright">

        </div>

    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script>
        $(function () {
            let i = 0
            let timer;
            function run() {
                timer = setInterval(function () {
                    if (++i === 7) i = 0
                    $('.wipper li').eq(i).show().siblings().hide()
                    $('.round li').eq(i).addClass('color').siblings('.color').removeClass('color')

                }, 2000)
            }
            run();
            $('.arrowleft').click(function () {
                clearInterval(timer)
                //清除定时器的作用是为了处理一些人为插入操作导致的轮播效果效果的不和谐,不流畅不自然情况
                if (--i < 0) i = 6
                $('.wipper li').eq(i).show().siblings().hide()
                $('.round li').eq(i).addClass('color').siblings('.color').removeClass('color')
                run()
                //完成操作时重新调用定时器
            })

            $('.arrowright').click(function () {
                clearInterval(timer)
                if (++i > 6) i = 0
                $('.wipper li').eq(i).show().siblings().hide()
                $('.round li').eq(i).addClass('color').siblings('.color').removeClass('color')
                run()
            })
            $('.round li').click(function () {
                clearInterval(timer)
                i = $(this).index();
                $('.wipper li').eq(i).show().siblings().hide()
                $('.round li').eq(i).addClass('color').siblings('.color').removeClass('color')
                run()
            })
        })
    </script>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值