Jquery实现轮播图

css实现效果

 .box {
            width: 600px;
            height: 300px;
            position: relative;
            margin: 0 auto;
            overflow: hidden;
        }
        
        .box-img {
            height: 300px;
            position: absolute;
            top: 0;
            left: 0;
        }
        
        .img-item {
            float: left;
            width: 600px;
            height: 300px;
            line-height: 300px;
            text-align: center;
            font-size: 100px;
        }
        
        .img-item:nth-child(1) {
            background: #00f;
        }
        
        .img-item:nth-child(2) {
            background: #0f0;
        }
        
        .img-item:nth-child(3) {
            background: #f0f;
        }
        
        .img-item:nth-child(4) {
            background: #ff0;
        }
        
        .img-item:nth-child(5) {
            background: #00f;
        }
        
        .box-btn {
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1;
        }
        
        .box-circle {
            position: absolute;
            bottom: 20px;
            left: 0;
            right: 0;
            text-align: center;
        }
        
        .circle-item {
            display: inline-block;
            width: 8px;
            height: 8px;
            border-radius: 50%;
            margin: 0 10px;
            background-color: #000;
        }
        
        .circle-item.active {
            background: #eee;
        }

html实现效果

 <div class="box">
        <div class="box-img">
            <div class="img-item">1</div>
            <div class="img-item">2</div>
            <div class="img-item">3</div>
            <div class="img-item">4</div>
        </div>
        <div class="box-circle"></div>
        <div class="box-btn">
            <button class="prev">上一页</button>
            <button class="next">下一页</button>
        </div>
    </div>

引入jquery.js

    https://code.jquery.com/jquery-3.6.0.min.js

 js代码

 <script src="./jquery-3.6.0.js"></script>
    <script>
        //克隆一张item放入box-img的后面
        function clone(ele, parent) {
            const _ele = ele.clone();
            _ele.appendTo(parent);
        }
        clone($(".img-item:first"), $(".box-img"));

        //设置box-img的宽度=item的宽度*item的长度
        let width = $(".img-item").width();
        $(".box-img").width($(".img-item").length * width);

        let index = 0;
        let flag = true; //可以防止点击次数过多flag=false为鼠标点击没有效果

        //创建指示器
        for (let i = 0; i < $(".img-item").length - 1; i++) {
            let item = $("<div class=\"circle-item\"></div>");
            console.log(i)
            if (i === 0) {
                item.addClass("active");
            }
            $(".box-circle").append(item)
        }

        function changeSlide(index) {
            $(".box-circle").children().eq(index).addClass("active").siblings(".active").removeClass("active");
        }

        //为下一页设置点击事件
        $(".box-btn").on("click", ".next", function() {
                if (flag) {
                    flag = false;
                    index++;
                    $(".box-img").animate({
                        left: -width * index
                    }, function() {
                        if (index === $(".img-item").length - 1) {
                            $(this).css({
                                left: 0
                            })
                            index = 0;
                        }
                        changeSlide(index)
                        flag = true;
                    })
                }
            })
            //为上一页设置点击事件
        $(".box-btn").on("click", ".prev", function() {
            if (flag) {
                flag = false;
                if (index === 0) {
                    index = $(".img-item").length - 1;
                    $(".box-img").css("left", "-width*index")
                }
                index--;
                $(".box-img").animate({
                    left: -width * index
                }, function() {
                    flag = true;
                    changeSlide(index)
                })

            }
        })
    </script>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值