当前位置:首页 > 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 库:

vue实现轮播图源码

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>

关键功能扩展

添加淡入淡出动画效果:

vue实现轮播图源码

.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>

添加触摸滑动支持:

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

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

相关文章

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &…

vue 实现脚本

vue 实现脚本

Vue 实现脚本的方法 Vue.js 提供了多种方式来实现脚本功能,包括组件内脚本、混入(Mixins)、插件(Plugins)以及自定义指令等。以下是常见的实现方式: 组件内脚本 在 Vue 单文…

vue实现radio

vue实现radio

Vue 实现 Radio 按钮 在 Vue 中实现 Radio 按钮可以通过 v-model 进行数据绑定,结合 input 元素的 type="radio" 属性。以下是一个基本示例: <t…

vue实现list

vue实现list

Vue 实现列表渲染 在 Vue 中,可以使用 v-for 指令来渲染列表。v-for 指令基于一个数组或对象来渲染一个列表,可以遍历数组或对象的属性。 基本列表渲染 <template>…

vue实现监听

vue实现监听

监听数据变化 在Vue中,可以通过watch选项或$watch方法监听数据的变化。watch适用于组件选项内声明式监听,$watch适用于动态监听。 // 选项式API export default…

vue实现录像

vue实现录像

Vue 实现录像功能 在 Vue 中实现录像功能通常需要借助浏览器的 MediaDevices API 和 MediaRecorder API。以下是实现步骤: 获取用户摄像头和麦克风权限 使用…