jquery实现轮播图

html代码

!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <link rel="stylesheet" href="./css/lunbo.css">
    <script src='./jquery/jquery-3.4.1.js'></script>
</head>

<body>
    <div class="content">
        <!--向左向右-->
        <span class="change left" id="left">&lt;</span>
        <span class="change right" id="right">&gt;</span>

        <!--轮播图片-->
        <ul class="picture" id="pictures">
            <li><img src="./img/1.jpg" class="1"></li>
            <li><img src="./img/6.jpg" class="2"></li>
            <li><img src="./img/7.jpg" class="3"></li>
            <li><img src="./img/12.jpg" class="4"></li>
            <li><img src="./img/17.jpg" class="5"></li>
        </ul>

        <!--提示圆点-->
        <div class="cloth">
            <ul class="dot" id="dots">
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
        </div>
    </div>

    <script>
        //当前图片下标
        var currentIdex = 0;
        showPicture()

        //点击下一张
        $('#right').click(function () {
            getRight()
            showPicture()
        })
        function getRight() {
            currentIdex++;
            if (currentIdex == $('img').length) {
                currentIdex = 0
            }
        }
        //点击上一张
        $('#left').click(function () {
            getLeft()
            showPicture()
        })

        function getLeft() {
            currentIdex--;
            if (currentIdex == -1) {
                currentIdex = $('img').length - 1
            }
        }
        //根据下标显示图片
        function showPicture() {
            for (var i = 0; i < $('img').length; i++) {
                if (i == currentIdex) {
                    $('.picture li').eq(i).attr('class', 'active')
                } else {
                    $('li').eq(i).removeClass('active')
                }
            }

            for (var i = 0; i < $('.dot li').length; i++) {
                if (i == currentIdex) {
                    $('.dot li').eq(i).attr('class', 'actives')
                } else {
                    $('.dot li').eq(i).removeClass('actives')
                }
            }
        }
        //每3秒换一张
        setInterval(function () {
            getRight()
            showPicture()

        }, 5000)

        //给按钮添加点击效果
        for (var i = 0; i < $('.dot li').length; i++) {
            $('.dot li').eq(i).attr('data-index', i)
        }

        for (var i = 0; i < $('.dot li').length; i++) {
            $('.dot li').click(function (event) {
                currentIdex =$(event.target).attr('data-index')
                showPicture()
            })
        }


    </script>
</body>

</html>

css

*{
    padding: 0px;
    margin: 0px;
    list-style: none;
}

/*主区域*/
.content{
    top: 50px;
    margin: auto;
    width: 1000px;
    height: 530px;
    position: relative;
}

/*点击左右*/
.change{
    z-index: 10;
    width: 50px;
    height: 50px;
    top: 240px;
    display: block;
    position: absolute;
    font-size: 25px;
    color: white;
}
.change:hover{
    background-color: dimgray;
    border-radius: 50%;
    opacity: .6;
}
.left{
    left: 0;
    text-align: center;
    line-height: 50px;
    font-weight: 500;
}
.right{
    right: 0;
    text-align: center;
    line-height: 50px;
    font-weight: 500;
}


/*图片区域*/
.content .picture{
    width: 100%;
    height: 100%;
}

.content .picture li{
    width: 100%;
    height: 100%;
    position: absolute;
    opacity: 0;

}

.content .picture li img{
    width: 100%;
    height: 100%;
}

.active{
    z-index: 9;
    opacity: 1!important;
    transition: all 2s;

}

/*圆点区*/
.cloth{
    width: 100%;
    height: 75px;
    bottom: 0px;
    background-color: lightblue;
    opacity: .3;
    z-index: 10;
    position: absolute;
}

.dot{
    margin: auto;
    height: 100%;
    width: 300px;
}

.dot li{
    display: inline-block;
    z-index: 10;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    border: black solid 1px;
    margin-left: 37px;
    margin-top: 35px;
}


.actives{
    background-color: black!important;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值