jquery之滑动轮播

9 篇文章 0 订阅

滑动轮播:

简单思路:利用两个嵌套的盒子,内部的盒子放图片,并设置绝对定位,设置左右两个按钮,定义一个变量,点击按钮时,变量加加,根据变量来让内部的盒子左右移动,就实现了滑动轮播。
代码如下所示:

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./jquery-3.4.1.js"></script>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    .box {
        position: relative;
        width: 600px;
        height: 300px;
        margin: 100px 0 0 100px;
        border: 1px solid #cdcdcd;
        overflow: hidden;
    }

    .move {
        position: absolute;
        width: 600px;
        height: 300px;
        display: flex;
        justify-content: space-between;
        transition: left 0.2s linear;
        left:0;
        /* overflow: hidden; */
    }

    .move img {
        width: 600px;
        height: 300px;
    }

    .btn {
        width: 100%;
        position: absolute;
        top: 50%;
    }

    .btn-left {
        display: block;
        float: left;
        width: 40px;
        height: 30px;
        background-color: rgba(0, 0, 0, 0.5);
        font-size: 25px;
        text-align: center;
        line-height: 30px;
        color: azure;

    }

    .btn-right {
        display: block;
        float: right;
        width: 40px;
        height: 30px;
        background-color: rgba(0, 0, 0, 0.5);
        font-size: 25px;
        text-align: center;
        line-height: 30px;
        color: azure;
    }

    .list {
        width: 100px;
        height: 20px;
        position: absolute;
        display: flex;
        justify-content: space-between;
        bottom: 20px;
        left: 230px;

    }

    .list li {
        list-style: none;
        width: 15px;
        height: 15px;
        border-radius: 50%;
        background-color: #fff;
    }
</style>

<body>
    <div class="box">
        <div class="move">
            <img src="./img/0.png" alt="">
            <img src="./img/1.png" alt="">
            <img src="./img/2.png" alt="">
            <img src="./img/3.png" alt="">
            <img src="./img/4.png" alt="">
        </div>
        <div class="btn">
            <span class="btn-left">
                <</span> <span class="btn-right">>
            </span>
        </div>
        <!-- <ul class="list">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul> -->
    </div>
</body>

</html>
<script>

    var count = 0;

    function next() {
        count++;
        if (count == $('.move img').length) {
            count = 0;
        }
     
        $('.move').css({
            left: count * -600 + 'px'
        });
     
        // console.log(count)

    }
    console.log(
$('.move img').index()
    );
    
    // var ll = next()    
    console.log(count)

    function prev() {
        count--;
        if (count == -1) {
            count = $('.move img').length - 1;
        }
        // console.log(count)
        $('.move').css({
            left: count * -600 + 'px'
        });
        
     

    }

    // function show() {
    //     $('.move').css({
    //         left: count * -600 + 'px'
    //     });
    // }

    $('.btn-right').click(function () {
        next();

        // console.log('aaa')
    })

 

    $('.btn-left').click(function () {
        prev();
    })
    setInterval(() => {
        next()
    }, 1000)



</script>

下面是效果截图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值