用Vue写一个简单的无缝轮播

无缝轮播的原理

头尾都是数组第一个然后控制父容器是否有过度动画 可以达到一个视觉差

如 原数组 [0,1,2,3];
无缝轮播数组[0,1,2,3,0];
在这里插入图片描述

全部代码

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dome</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
</head>
<style>
    * {
        margin: 0px;
        padding: 0px;
    }

    .BannerBlock {
        width: 650px;
        height: 250px;
        background: blanchedalmond;
        margin: auto;
        overflow: hidden;
    }

    .Banner_block {
        width: fit-content;
        height: 240px;
        display: flex;
    }

    .Banner_block>.item {
        width: 650px;
        height: 240px;
        background: lightgreen;
        display: flex;
        justify-content: center;
        align-items: center;
    }
</style>

<body>
    <div id="app">
        <div class="BannerBlock">
            <div class="Banner_block"
                :style="{marginLeft:Lfet+'px',width:bannerdata.length-1*650+'px',transition:isTr?'all 0.5s':'none'}">
                <div class="item" v-for="(i,index) in bannerdata" :key='index'>
                    {{i}}
                </div>
            </div>
        </div>
        <div @click='Bannerrigth'>上一个</div>
        <div @click='BannerLfet'>下一个</div>
    </div>
    <script type="text/javascript">
        var app = new Vue({
            el: '#app',
            data: {
                bannerdata: [1, 2, 3,4,5,6,7,8,9,1],
                message: 'Hello Vue!',
                isTr: true,
                eindex: 0,
                Lfet: 0,
                isclick: false
            },
            created() {

            },
            methods: {
                Bannerrigth() {
                    if (this.eindex == 0) {
                        this.isTr = false;
                        this.eindex = this.bannerdata.length - 1;
                        this.Lfet = -this.eindex * 650;
                        setTimeout(res => {
                            this.isTr = true;
                            this.eindex--;
                            this.Lfet = -this.eindex * 650;
                        }, 10)
                    } else {
                        this.eindex--;
                        this.Lfet = -this.eindex * 650;
                    }
                },
                BannerLfet() {
                    this.isTr = true;
                    this.eindex++;
                    this.Lfet = -this.eindex * 650;
                    if (this.eindex == this.bannerdata.length - 1) {
                        setTimeout(res => {
                            this.eindex = 0;
                            this.isTr = false;
                            this.Lfet = -this.eindex * 650;
                        }, 500)
                    }
                }
            },
        })
    </script>
</body>
<script type="text/javascript">

</script>

</html>

全代码奉上 直接复制可用 逻辑简单 有问题可评论区问

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值