当前位置:首页 > VUE

vue实现轮播图源码

2026-02-23 09:27:14VUE

Vue 轮播图实现方法

使用 Vue 3 的 Composition API 和 <transition-group> 实现基础轮播图:

<template>
  <div class="carousel-container">
    <button @click="prevSlide">上一张</button>
    <transition-group name="slide" tag="div" class="slides">
      <div v-for="(slide, index) in slides" 
           :key="index"
           v-show="currentIndex === index"
           class="slide">
        <img :src="slide.image" :alt="slide.title">
      </div>
    </transition-group>
    <button @click="nextSlide">下一张</button>
    <div class="dots">
      <span v-for="(dot, index) in slides" 
            :key="index"
            @click="goToSlide(index)"
            :class="{ active: currentIndex === index }">
      </span>
    </div>
  </div>
</template>

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

const slides = ref([
  { image: 'image1.jpg', title: '图片1' },
  { image: 'image2.jpg', title: '图片2' },
  { image: 'image3.jpg', title: '图片3' }
])

const currentIndex = ref(0)
const autoPlay = ref(true)
const interval = ref(3000)

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

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

const goToSlide = (index) => {
  currentIndex.value = index
}

let timer

onMounted(() => {
  if (autoPlay.value) {
    timer = setInterval(nextSlide, interval.value)
  }
})
</script>

<style>
.carousel-container {
  position: relative;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  overflow: hidden;
}

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

.slide {
  width: 100%;
  flex-shrink: 0;
}

.slide img {
  width: 100%;
  height: auto;
}

.dots {
  text-align: center;
  margin-top: 10px;
}

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

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

.slide-enter-active, .slide-leave-active {
  transition: opacity 0.5s;
}
.slide-enter-from, .slide-leave-to {
  opacity: 0;
}
</style>

使用第三方库实现

安装 vue-awesome-swiper 库:

npm install swiper vue-awesome-swiper

组件实现代码:

<template>
  <swiper
    :options="swiperOptions"
    :auto-update="true"
    :auto-destroy="true"
    @ready="onSwiperReady"
    @slideChange="onSlideChange"
  >
    <swiper-slide v-for="(slide, index) in slides" :key="index">
      <img :src="slide.image" :alt="slide.title">
    </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', title: '图片1' },
        { image: 'image2.jpg', title: '图片2' },
        { image: 'image3.jpg', title: '图片3' }
      ],
      swiperOptions: {
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        },
        loop: true,
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        }
      }
    }
  },
  methods: {
    onSwiperReady(swiper) {
      console.log('Swiper ready', swiper)
    },
    onSlideChange() {
      console.log('Slide changed')
    }
  }
}
</script>

<style>
.swiper-container {
  width: 100%;
  height: 300px;
}
.swiper-slide img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>

关键功能扩展

添加淡入淡出动画效果:

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter-from, .fade-leave-to {
  opacity: 0;
}

实现缩略图导航:

<div class="thumbnails">
  <img 
    v-for="(slide, index) in slides" 
    :key="index"
    :src="slide.image"
    @click="goToSlide(index)"
    :class="{ active: currentIndex === index }">
</div>

添加触摸滑动支持:

vue实现轮播图源码

const swiperOptions = {
  // ...
  touchEventsTarget: 'container',
  simulateTouch: true,
  touchRatio: 1,
  touchAngle: 45,
  grabCursor: true
}

标签: 源码vue
分享给朋友:

相关文章

vue tag实现

vue tag实现

Vue 标签实现方法 在 Vue 中实现标签功能可以通过多种方式完成,常见场景包括动态标签生成、标签输入框、标签管理等。以下是几种典型实现方案: 动态标签列表渲染 使用 v-for 指令渲染标签数组…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <ul&g…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue拖拽实现

vue拖拽实现

Vue 拖拽实现方法 使用 HTML5 原生拖拽 API HTML5 提供了原生拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop 事…

vue实现波形

vue实现波形

实现波形效果的方法 在Vue中实现波形效果可以通过多种方式完成,常见的方法包括使用Canvas绘制、CSS动画或第三方库。以下是几种实现方案: 使用Canvas绘制波形 通过Canvas API动态…

vue nexttrick实现

vue nexttrick实现

Vue 中 nextTick 的实现原理 nextTick 是 Vue 提供的一个异步方法,用于在下次 DOM 更新循环结束之后执行延迟回调。它利用了 JavaScript 的事件循环机制,确保回调在…