Vue实现轮播图效果

1 上一项和下一项自己添加样式吧.

需要注意的事v-for 后面要加:key值
其他的看代码吧

<template lang="html">
  <div class="banner" @mouseover="endAutoPlay()" @mouseout="startAutoPlay()">
    <a href="javascript:;" class="btn prev" @click="prev()">上一项</a>
    <a href="javascript:;" class="btn next" @click="next()">下一项</a>
    <ul class="banner-list">
      <li class="banner-container" :class="{cur: index==cur}" v-for="banner,index in banners" :key="index">
        <a href="javascript:;">
          <img :src="banner.src">
        </a>
      </li>
    </ul>
    <ol class="number-list">
      <li :class="{active: index==cur}" v-for="banner,index in banners" :key="index" @mouseover="cur=index"></li>
    </ol>
  </div>
</template>

<script>
import img1 from '@/assets/banner1.jpeg'
import img2 from '@/assets/banner2.jpeg'
import img3 from '@/assets/banner3.jpeg'
export default {
  data () {
    return {
      cur: 0,
      timer: 300,
      banners: [{ src: img1 }, { src: img2 }, { src: img3 }]

    }
  },
  created () {
    this.startAutoPlay()
  },
  beforeDestroy () {
    this.endAutoPlay()
  },
  methods: {
    prev () {
      if (this.cur === 0) {
        this.cur = this.banners.length - 1
      } else {
        this.cur--
      }
    },
    next () {
      if (this.cur === this.banners.length - 1) {
        this.cur = 0
      } else {
        this.cur++
      }
    },
    startAutoPlay () {
      this.timer = setInterval(this.next, 2000)
    },
    endAutoPlay () {
      clearInterval(this.timer)
    }
  }
}
</script>

<style>
/* banner */
.banner {
  height: 420px;
  position: relative;
  overflow: hidden;
}
.banner-container {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 1;
  opacity: 0;
  transition: 0.7s all ease;
}
.banner-container img {
  width: 1920px;
  height: 420px;
}
.banner-container.cur {
  opacity: 1;
}

.banner .btn {
  position: absolute;
  z-index: 2;
  left: 50%;
  top: 200px;
  text-indent: -99999px;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: #d0c4af;
  background-image: url(../assets/u1.png);
  background-repeat: no-repeat;
}
.banner .btn:hover {
  background-color: #b19e7a;
}
.banner .btn.prev {
  background-position: 0 -5384px;
  margin-left: -605px;
}
.banner .btn.next {
  background-position: 0 -5604px;
  margin-left: 555px;
}
.banner .number-list {
  position: absolute;
  left: 0;
  width: 100%;
  bottom: 4px;
  text-align: center;
  z-index: 2;
}
.banner .number-list li {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin: 0 8px 10px;
  cursor: pointer;
  background: #fff;
  border-radius: 50%;
  border: 1px solid #cecece;
  transition: 0.7s all ease;
}
.banner .number-list .active {
  background: #a7936e;
  box-shadow: 0 0 0 4px #dfcead;
}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值