vue无缝轮播案例

效果图

 

利用vue提供的transtion标签实现动画

css代码;

    <style>
        #app{
            width: 400px;
            height: 300px;
            margin: 50px auto;
            position: relative;
            text-align: center;
            overflow: hidden;
        }
        img{
            width: 100%;
            height: 100%;
            position: absolute;
            top: 30px;
            left: 0;
        }
        /* 下一张的动画效果 */
        .next-enter{
            left: 400px;
        }
        .next-enter-active{
            transition: all 1s;
        }
        .next-enter-to{
            left: 0px;
        }
        .next-leave{
            left: 0px;
        }
        .next-leave-active{
            transition: all 1s;
        }
        .next-leave-to{
            left: -400px;
        }
        /* 上一张动画效果 */
        .pre-enter{
            left: -400px;
        }
        .pre-enter-active{
            transition: all 1s;
        }
        .pre-enter-to{
            left: 0px;
        }
        .pre-leave{
            left: 0;
        }
        .pre-leave-active{
            transition: all 1s;
        }
        .pre-leave-to{
            left: 400px;
        }
    </style>

html代码:

这里小编钻了漏子,利用小编的图片共有5张,并且命名时1,2,3,4,5,所以利用v-for遍历了数字,然后拼接图片路径,如果看不懂可以联系我。

<div id="app">
        <button @click="page(-1)">上一页</button>
        <button @click="page(1)">下一页</button>
        <transition :name="aniName">
            <img :src=`../image/${n}.webp` alt="" v-for="n in 5" :key="n" v-if="n==current" @mouseover="f1" @mouseout="f2">
        </transition>

    </div>

js代码:

<script>
    var app = new Vue({
        el:"#app",
        data:{
            current:1,//播放第几张图片
            aniName:"",//过渡动画的名字
            isChange:true,
            timer:null
        },
        created() {
            this.timer = setInterval(() => {
                this.page(1)
            }, 2000);
        },
        methods: {
            f1(){
                clearInterval(this.timer)
            },
            f2(){
                this.timer = setInterval(() => {
                this.page(1)
            }, 2000);
            },
            page(offset){
                // 这里的isChange是为了防抖,避免连续多次点击
                if (this.isChange==false) {
                    return
                }else{
                    this.isChange = false
                }
                this.current = this.current+offset
                // 这里虽然只有5张图片,但是如果遍历到5的时候就返回第一张,那么第五张图片的动画没走完就返回第一张了,所以要遍历到6,这样可以实现无缝轮播。
                if (this.current>=6) {
                    this.current = 1
                }
                if (this.current<=0) {
                    this.current = 5
                }
                if (offset==1) {
                    this.aniName = "next"
                }else{
                    this.aniName = "pre"
                }
                
                setTimeout(()=>{
                    this.isChange = true
                },500)
            }
        },
    })

</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Splide是一款用TypeScript编写的轻量级轮播图组件,它没有任何依赖,代码简单清晰,体积小巧。它提供了多种轮播组合,包括单图轮播、N图轮播、聚焦轮播、分页轮播、垂直轮播等。它的轮播过程动画非常细腻,滑动效果流畅顺滑,末尾图片反弹效果也很流畅。\[1\] 在Vue3中使用Vue Splide进行轮播图滑动无缝的配置步骤如下: 第一步:安装配置 在main.ts文件中,使用以下代码安装Vue Splide: ```javascript import { createApp } from 'vue' import VueSplide from '@splidejs/vue-splide' const app = createApp(App) app.use(VueSplide) ``` 第二步:在Demo.vue文件中使用Vue Splide进行轮播图滑动无缝的示例代码如下: ```html <template> <Splide :options="{ rewind: true }"> <SplideSlide> <img src="http://localhost:9090/wego/ad/c1.jpg"> </SplideSlide> <SplideSlide> <img src="http://localhost:9090/wego/ad/c2.jpg"> </SplideSlide> <SplideSlide> <img src="http://localhost:9090/wego/ad/c3.jpg"> </SplideSlide> <SplideSlide> <img src="http://localhost:9090/wego/ad/c4.jpg"> </SplideSlide> <SplideSlide> <img src="http://localhost:9090/wego/ad/c5.jpg"> </SplideSlide> </Splide> </template> <script lang="ts" setup> import { Splide, SplideSlide } from '@splidejs/vue-splide' import '@splidejs/splide/dist/css/themes/splide-default.min.css' </script> ``` 以上是在Vue3中使用Vue Splide进行轮播图滑动无缝的配置和示例代码。你可以根据自己的需求进行相应的修改和定制。 #### 引用[.reference_title] - *1* *2* [Vue3轮播图插件 vue-splide](https://blog.csdn.net/lianghecai52171314/article/details/125818392)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值