用vue写一个轮播图

<template>
  <div class="banner"  @mouseenter="enter" @mouseleave="leave">
    <div class="item">
      <img :src="dataList[currentIndex]" />
    </div>
    <div class="page" v-if="this.dataList.length > 1">
      <ul>
        <li @click="gotoPage(prevIndex)">&lt;</li>
        <li
          v-for="(item, index) in dataList" :key="index"
          @click="gotoPage(index)"
          :class="{ current: currentIndex === index }"
        >
          {{ index + 1 }}
        </li>
        <li @click="gotoPage(nextIndex)">&gt;</li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      dataList: [
        'https://i1.mifile.cn/a4/xmad_15535933141925_ulkYv.jpg',
        'https://i1.mifile.cn/a4/xmad_15532384207972_iJXSx.jpg',
        'https://i1.mifile.cn/a4/xmad_15517939170939_oiXCK.jpg'
      ],
      currentIndex: 0, // 默认显示图片
      timer: null // 定时器
    }
  },
  created () {
    this.runInv()
  },
  methods: {
    gotoPage (index) {
      this.currentIndex = index
    },
    // 定时器
    runInv () {
      this.timer = setInterval(() => {
        this.gotoPage(this.nextIndex)
      }, 2000)
    },
    // 鼠标移入
    enter () {
      clearInterval(this.timer)
    },
    // 鼠标移出
    leave () {
      this.runInv()
    }
  },
  computed: {
    // 上一张
    prevIndex () {
      if (this.currentIndex === 0) {
        return this.dataList.length - 1
      } else {
        return this.currentIndex - 1
      }
    },
    // 下一张
    nextIndex () {
      if (this.currentIndex === this.dataList.length - 1) {
        return 0
      } else {
        return this.currentIndex + 1
      }
    }
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
}
ul li {
  list-style: none;
  float: left;
  width: 30px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  cursor: pointer;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
}
.banner {
  max-width: 1200px;
  margin: 0 auto;
  position: relative;
  margin-top: 60px;
}
.banner img {
  width: 100%;
  display: block;
}
.banner .page {
  background: rgba(0, 0, 0, 0.5);
  position: absolute;
  right: 0;
  bottom: 0;
  width: 100%;
}
.banner .page ul {
  float: right;
}
.current {
  color: #ff6700;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的使用 Vue 实现轮播图的例子: ```html <template> <div class="slider"> <ul class="slider-list" :style="{transform: 'translateX(' + translateX + 'px)'}"> <li class="slider-item" v-for="item in items" :key="item.id"> <img :src="item.imgUrl" alt=""> </li> </ul> <ul class="slider-dots"> <li class="slider-dot" v-for="(item, index) in items" :key="item.id" :class="{active: currentIndex === index}" @click="goTo(index)"></li> </ul> </div> </template> <script> export default { data() { return { items: [ {id: 1, imgUrl: 'https://picsum.photos/id/1/600/400'}, {id: 2, imgUrl: 'https://picsum.photos/id/2/600/400'}, {id: 3, imgUrl: 'https://picsum.photos/id/3/600/400'}, ], currentIndex: 0, translateX: 0, timer: null, interval: 2000, duration: 500, } }, mounted() { this.start() }, methods: { start() { this.timer = setInterval(() => { this.next() }, this.interval) }, next() { this.goTo(this.currentIndex + 1) }, goTo(index) { if (index >= this.items.length) { index = 0 } else if (index < 0) { index = this.items.length - 1 } this.currentIndex = index this.translateX = -index * this.$refs.list.offsetWidth clearInterval(this.timer) setTimeout(() => { this.start() }, this.duration) }, }, } </script> <style> .slider { position: relative; width: 600px; height: 400px; overflow: hidden; margin: 0 auto; } .slider-list { display: flex; align-items: center; height: 100%; transition: transform 0.5s ease-in-out; } .slider-item { flex: 0 0 600px; height: 400px; } .slider-dots { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; } .slider-dot { width: 10px; height: 10px; border-radius: 50%; margin: 0 5px; background-color: #ccc; cursor: pointer; } .slider-dot.active { background-color: #333; } </style> ``` 在这个例子中,我们使用 `translateX` 属性来实现轮播图的滑动效果,使用定时器来自动播放,同时也支持手动切换。其中 `interval` 和 `duration` 分别表示自动播放的时间间隔和切换的动画时长,可以根据需要进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值