当前位置:首页 > VUE

vue实现动态轮播图

2026-01-23 05:40:05VUE

Vue 动态轮播图实现方法

使用 Vue 3 + Swiper 实现

安装 Swiper 和 Vue 相关依赖:

npm install swiper vue-awesome-swiper

组件代码示例:

<template>
  <swiper
    :modules="modules"
    :slides-per-view="1"
    :space-between="50"
    navigation
    :pagination="{ clickable: true }"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide v-for="(slide, index) in slides" :key="index">
      <img :src="slide.image" :alt="slide.title">
      <h3>{{ slide.title }}</h3>
    </swiper-slide>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'swiper/vue'
import { Navigation, Pagination } from 'swiper/modules'
import 'swiper/css'
import 'swiper/css/navigation'
import 'swiper/css/pagination'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  setup() {
    const slides = ref([
      { image: '/image1.jpg', title: 'Slide 1' },
      { image: '/image2.jpg', title: 'Slide 2' },
      { image: '/image3.jpg', title: 'Slide 3' }
    ])

    return {
      modules: [Navigation, Pagination],
      slides
    }
  }
}
</script>

使用 Vue 2 + Vue-Awesome-Swiper

安装依赖:

vue实现动态轮播图

npm install swiper vue-awesome-swiper@3.1.3

组件代码示例:

<template>
  <swiper :options="swiperOption">
    <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>
  </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: 'Slide 1' },
        { image: '/image2.jpg', title: 'Slide 2' }
      ],
      swiperOption: {
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        },
        autoplay: {
          delay: 3000
        }
      }
    }
  }
}
</script>

纯 Vue 实现轮播图

不使用第三方库的简单实现:

vue实现动态轮播图

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

<script>
export default {
  data() {
    return {
      slides: [
        { image: '/image1.jpg', title: 'Slide 1' },
        { image: '/image2.jpg', title: 'Slide 2' }
      ],
      currentIndex: 0,
      interval: null
    }
  },
  mounted() {
    this.startAutoPlay()
  },
  beforeDestroy() {
    clearInterval(this.interval)
  },
  methods: {
    startAutoPlay() {
      this.interval = setInterval(() => {
        this.next()
      }, 3000)
    },
    next() {
      this.currentIndex = (this.currentIndex + 1) % this.slides.length
    },
    prev() {
      this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
    }
  }
}
</script>

<style>
.carousel {
  overflow: hidden;
  position: relative;
}
.slides {
  display: flex;
  transition: transform 0.5s ease;
}
.slide {
  min-width: 100%;
}
</style>

动态加载数据

从 API 获取轮播图数据:

<script>
export default {
  data() {
    return {
      slides: [],
      loading: true
    }
  },
  async created() {
    try {
      const response = await fetch('https://api.example.com/slides')
      this.slides = await response.json()
    } catch (error) {
      console.error('Error fetching slides:', error)
    } finally {
      this.loading = false
    }
  }
}
</script>

添加过渡动画

为轮播图添加平滑过渡效果:

<template>
  <transition-group name="slide" tag="div" class="carousel">
    <div v-for="(slide, index) in slides" 
         :key="index"
         v-show="index === currentIndex"
         class="slide">
      <img :src="slide.image" :alt="slide.title">
    </div>
  </transition-group>
</template>

<style>
.slide-enter-active, .slide-leave-active {
  transition: opacity 0.5s, transform 0.5s;
}
.slide-enter {
  opacity: 0;
  transform: translateX(100%);
}
.slide-leave-to {
  opacity: 0;
  transform: translateX(-100%);
}
</style>

标签: 动态vue
分享给朋友:

相关文章

vue架构实现

vue架构实现

Vue 架构实现核心方案 Vue.js 的架构实现通常围绕模块化、组件化和状态管理展开。以下为常见实现方案: 基于单文件组件(SFC)的模块化 通过 .vue 文件组织模板、脚本和样式: <…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show…

实现vue框架

实现vue框架

Vue框架的基本实现 Vue是一个渐进式JavaScript框架,用于构建用户界面。以下是实现Vue框架的基本步骤: 安装Vue 通过CDN引入Vue是最简单的方式: <script src…

vue实现Pop

vue实现Pop

Vue 实现 Popover 组件的方法 使用 Vue 内置指令 v-show/v-if 和事件监听 通过 Vue 的指令和事件绑定实现基础的 Popover 功能。定义一个布尔值控制 Popover…