用vue写一个简单轮播图效果

轮播图在不同的移动设备随处可见,可以增添页面几分美观等优点,接下来用渐进式框架vue来实现轮播图效果

一、原理:
创建数组backgroundImg作为轮播图图片路径,声明变量timer为时间,swing=0为默认的第一张图片。通过定时器或点击来切换图片效果。
二、变量:
      swing: 0,
      timer: null,
      //背景图
       backgroundImg: [
        "background:url(https://imgessl/commendpic/20210831/20210831233709628572.jpg) no-repeat center top;",
        "background:url(https://imgessl/commendpic/20200407/20200407114244160948.jpg) no-repeat center top;",
        "background:url(https://imgessl/commendpic/20210831/20210831233709628572.jpg)no-repeat center top; ",

      ],

三、 在计算属性computed中声明两个方法proIndex和nextIndex,来做加减档操作,

   //computed
  computed: {
   
    //上一张
    proIndex() {
   
      if (this.swing == 0) {
   
        return this.backgroundImg.length - 1;
      } else {
   
        return this.swing - 1
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的层叠轮播图实现,使用Vue的transition和v-for指令: HTML: ``` <template> <div class="carousel"> <transition-group name="carousel" tag="div"> <div v-for="(item, index) in items" :key="index" class="carousel-item" :style="{ zIndex: index }"> <img :src="item"> </div> </transition-group> </div> </template> ``` CSS: ``` .carousel { position: relative; width: 100%; height: 400px; } .carousel-item { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; transition: opacity 0.5s ease-in-out; } .carousel-item.active { opacity: 1; } .carousel-enter-active, .carousel-leave-active { transition: opacity 0.5s ease-in-out; } .carousel-enter, .carousel-leave-to { opacity: 0; } ``` JavaScript: ``` <script> export default { data() { return { items: [ 'https://picsum.photos/800/400?random=1', 'https://picsum.photos/800/400?random=2', 'https://picsum.photos/800/400?random=3', 'https://picsum.photos/800/400?random=4', 'https://picsum.photos/800/400?random=5', ], current: 0, }; }, mounted() { setInterval(() => { this.current = (this.current + 1) % this.items.length; }, 3000); }, }; </script> ``` 在这个实现中,我们使用了一个数组来存储轮播图的图片链接。在mounted生命周期钩子中,我们使用setInterval来定时更新current变量,以便实现轮播。然后,我们使用v-for指令来循环渲染轮播图中的每个图片,使用transition-group和transition指令来实现动画效果。在CSS中,我们定义了.carousel-item样式来设置轮播图的位置和透明度,并使用了动画过渡效果。注意,我们使用了一个active类来设置当前轮播图的透明度为1。最后,在JavaScript中,我们将current变量绑定到轮播图上,以便在每个轮播图上使用active类。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值