当前位置:首页 > VUE

vue实现简单轮播图

2026-02-25 07:53:47VUE

Vue 实现简单轮播图

使用 Vue 3 的 Composition API 实现基础轮播图

<template>
  <div class="carousel-container">
    <div class="carousel" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
      <div class="slide" v-for="(slide, index) in slides" :key="index">
        <img :src="slide.image" :alt="slide.alt">
      </div>
    </div>
    <button @click="prevSlide">Previous</button>
    <button @click="nextSlide">Next</button>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';

const slides = ref([
  { image: 'image1.jpg', alt: 'Image 1' },
  { image: 'image2.jpg', alt: 'Image 2' },
  { image: 'image3.jpg', alt: 'Image 3' }
]);

const currentIndex = ref(0);

const nextSlide = () => {
  currentIndex.value = (currentIndex.value + 1) % slides.value.length;
};

const prevSlide = () => {
  currentIndex.value = (currentIndex.value - 1 + slides.value.length) % slides.value.length;
};

onMounted(() => {
  setInterval(nextSlide, 3000);
});
</script>

<style>
.carousel-container {
  overflow: hidden;
  position: relative;
  width: 100%;
}

.carousel {
  display: flex;
  transition: transform 0.5s ease;
}

.slide {
  min-width: 100%;
}

.slide img {
  width: 100%;
  height: auto;
}
</style>

使用 Vue 2 的 Options API 实现

<template>
  <div class="carousel">
    <transition-group name="slide" tag="div">
      <div v-for="(item, index) in slides" :key="index" v-show="currentSlide === index">
        <img :src="item.image" :alt="item.alt">
      </div>
    </transition-group>
    <button @click="prev">Prev</button>
    <button @click="next">Next</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentSlide: 0,
      slides: [
        { image: 'image1.jpg', alt: 'Image 1' },
        { image: 'image2.jpg', alt: 'Image 2' },
        { image: 'image3.jpg', alt: 'Image 3' }
      ]
    };
  },
  methods: {
    next() {
      this.currentSlide = (this.currentSlide + 1) % this.slides.length;
    },
    prev() {
      this.currentSlide = (this.currentSlide - 1 + this.slides.length) % this.slides.length;
    }
  },
  mounted() {
    setInterval(this.next, 3000);
  }
};
</script>

<style>
.carousel {
  position: relative;
  overflow: hidden;
  width: 100%;
}

.slide-enter-active, .slide-leave-active {
  transition: all 1s ease;
}
.slide-enter {
  transform: translateX(100%);
}
.slide-leave-to {
  transform: translateX(-100%);
}

img {
  width: 100%;
}
</style>

添加指示器(小圆点导航)

在 Vue 3 版本中添加以下代码:

vue实现简单轮播图

<div class="indicators">
  <span 
    v-for="(_, index) in slides" 
    :key="index" 
    @click="currentIndex = index"
    :class="{ active: currentIndex === index }"
  ></span>
</div>

<style>
.indicators {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}

.indicators span {
  width: 12px;
  height: 12px;
  margin: 0 5px;
  border-radius: 50%;
  background-color: #ccc;
  cursor: pointer;
}

.indicators span.active {
  background-color: #333;
}
</style>

实现无限循环效果

修改 Vue 3 的 nextSlide 和 prevSlide 方法:

vue实现简单轮播图

const nextSlide = () => {
  if (currentIndex.value >= slides.value.length - 1) {
    currentIndex.value = 0;
  } else {
    currentIndex.value++;
  }
};

const prevSlide = () => {
  if (currentIndex.value <= 0) {
    currentIndex.value = slides.value.length - 1;
  } else {
    currentIndex.value--;
  }
};

使用第三方库 vue-awesome-swiper

安装:

npm install swiper vue-awesome-swiper

示例代码:

<template>
  <swiper :options="swiperOptions">
    <swiper-slide v-for="(slide, index) in slides" :key="index">
      <img :src="slide.image" :alt="slide.alt">
    </swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
    <div class="swiper-button-prev" slot="button-prev"></div>
    <div class="swiper-button-next" slot="button-next"></div>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      slides: [
        { image: 'image1.jpg', alt: 'Image 1' },
        { image: 'image2.jpg', alt: 'Image 2' },
        { image: 'image3.jpg', alt: 'Image 3' }
      ],
      swiperOptions: {
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        },
        loop: true,
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        }
      }
    }
  }
}
</script>

标签: 简单vue
分享给朋友:

相关文章

vue怎么实现动画

vue怎么实现动画

Vue 动画实现方法 Vue 提供了多种方式实现动画效果,包括内置过渡系统、第三方库集成以及 CSS 动画。以下是常见实现方法: 使用 Vue 过渡系统 通过 <transition>…

vue实现无线滚动列表

vue实现无线滚动列表

无限滚动列表的实现方法 在Vue中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件:…

vue实现文章锚点定位

vue实现文章锚点定位

实现文章锚点定位的方法 在Vue中实现文章锚点定位可以通过多种方式完成,以下是几种常见的方法: 使用原生HTML锚点 通过HTML的id属性和<a>标签的href属性实现锚点跳转。在Vu…

vue实现store

vue实现store

Vue 实现 Store 在 Vue 中,可以通过 Vuex 或 Pinia 实现全局状态管理(Store)。以下是两种主流方案的实现方法。 使用 Vuex 实现 Store Vuex 是 Vue…

vue 实现级联

vue 实现级联

Vue 实现级联选择器 级联选择器(Cascader)常用于省市区选择、分类选择等场景。Vue 中可以通过 Element UI、Ant Design Vue 等 UI 库实现,也可以手动封装。…

vue实现剪辑

vue实现剪辑

Vue 实现视频剪辑功能 在Vue中实现视频剪辑功能通常需要结合HTML5的<video>元素和第三方库(如FFmpeg.js或Video.js)。以下是几种常见的实现方法: 使用FF…