以下是使用Vue实现轮播图的示例代码:

HTML模板:

<template>
  <div class='carousel'>
    <ul class='carousel-list'>
      <li v-for='(item, index) in items' :key='index' :class='{ active: index === currentIndex }'>
        <img :src='item.imgUrl' alt=''>
      </li>
    </ul>
    <div class='carousel-dots'>
      <span v-for='(item, index) in items' :key='index' :class='{ active: index === currentIndex }' @click='handleDotClick(index)'></span>
    </div>
    <button class='carousel-btn prev' @click='handlePrevClick'><</button>
    <button class='carousel-btn next' @click='handleNextClick'>></button>
  </div>
</template>

CSS样式:

.carousel {
  position: relative;
}

.carousel-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

.carousel-list li {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transition: opacity 0.5s;
  opacity: 0;
}

.carousel-list li.active {
  opacity: 1;
}

.carousel-list li img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.carousel-dots {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
}

.carousel-dots span {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #ccc;
  margin-right: 10px;
  cursor: pointer;
}

.carousel-dots span.active {
  background-color: #333;
}

.carousel-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.5);
  color: #fff;
  font-size: 16px;
  cursor: pointer;
}

.carousel-btn.prev {
  left: 10px;
}

.carousel-btn.next {
  right: 10px;
}

JavaScript代码:

<script>
  export default {
    data() {
      return {
        currentIndex: 0, // 当前索引
        items: [
          { imgUrl: 'https://picsum.photos/id/1018/200/300' },
          { imgUrl: 'https://picsum.photos/id/1019/200/300' },
          { imgUrl: 'https://picsum.photos/id/1020/200/300' },
          { imgUrl: 'https://picsum.photos/id/1021/200/300' },
          { imgUrl: 'https://picsum.photos/id/1022/200/300' },
        ],
      };
    },
    mounted() {
      this.startCarousel();
    },
    methods: {
      // 播放轮播图
      startCarousel() {
        this.timer = setInterval(() => {
          this.handleNextClick();
        }, 3000);
      },
      // 停止轮播图
      stopCarousel() {
        clearInterval(this.timer);
      },
      // 切换到上一张图片
      handlePrevClick() {
        this.currentIndex--;
        if (this.currentIndex < 0) {
          this.currentIndex = this.items.length - 1;
        }
      },
      // 切换到下一张图片
      handleNextClick() {
        this.currentIndex++;
        if (this.currentIndex >= this.items.length) {
          this.currentIndex = 0;
        }
      },
      // 切换到指定索引的图片
      handleDotClick(index) {
        this.currentIndex = index;
      },
    },
  };
</script>

使用Vue实现轮播图的关键点是通过绑定数据来控制当前显示的图片,并通过CSS样式来实现图片的淡入淡出效果。同时,还需要实现切换图片、停止播放、播放轮播图、切换到指定图片等功能。

Vue轮播图实现:完整代码示例与关键点解析

原文地址: https://www.cveoy.top/t/topic/mHwa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录