vue简单轮播图效果

html

<div class="carousel-wrap" id="carousel">
          <transition-group tag="ul" class='slide-ul' name="list">
              <li v-for="(list,index) in imgArray" :key="index" v-show="index===currentIndex" @mouseenter="stop" @mouseleave="go">
                  <img :src="list">
              </li>
          </transition-group>
          <div class="carousel-items">
              <span v-for="(item,index) in imgArray.length" :class="{'active':index===currentIndex}" @mouseover="change(index)"></span>
          </div>
</div>

js数据

data() {
    return {
      imgArray: [ 
        require(`../assets/lunbo.png`),
        require(`../assets/banner-fill.jpg`)//直接写路径图片出不来
      ],
      currentIndex: 0,
      timer: ''
    };
  }

方法

methods: {
    go() {
        this.timer = setInterval(() => {
            this.autoPlay()
        }, 4000)
    },
    stop() {
        clearInterval(this.timer)
        this.timer = null
    },
    change(index) {
        this.currentIndex = index
    },
    autoPlay() {
        this.currentIndex++
        if ( this.currentIndex > this.imgArray.length-1) {
            this.currentIndex = 0
        }
    }
},
created() {
   this.$nextTick(() => {
        this.timer = setInterval(() => {
             this.autoPlay()
           }, 4000)
         })
       
}

css

.carousel-wrap {
   height: 224px;
   width: 100%;
   overflow: hidden;
   background-color: #fff;
}
.carousel-wrap img{
  width: 100%;
  height: 100%;
}
.slide-ul {
  position: relative;
  width: 100%;
  height: 100%;
}
.slide-ul li {
  position: absolute;
  width: 100%;
  height: 100%;
}
.carousel-items {
  position: absolute;
  z-index: 10;
  top: 320px;
  width: 100%;
  margin: 0 auto;
  text-align: center;
  font-size: 0;
}
.carousel-items span {
  display: inline-block;
  height: 6px;
  width: 30px;
  margin: 0 3px;
  background-color: #b2b2b2;
  cursor: pointer;
}
.carousel-items .active {
  background-color: orange;
}
.list-enter-to {
  transition: all 1s ease;
  transform: translateX(0);
}
.list-leave-active {
  transition: all 1s ease;
  transform: translateX(-100%)
}
.list-enter {
  transform: translateX(100%)
}
.list-leave {
  transform: translateX(0)
}

代码是网上各种地方找来改的,用来拯救快秃还没用的脑袋

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值