当前位置:首页 > 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 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是 Vue 实现的主要技术细节。 响应式系统 Vue 的响…

实现vue框架

实现vue框架

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

vue实现门户

vue实现门户

Vue 实现门户网站的关键步骤 搭建基础框架 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 管理多页面路由。门户通常需要响应式布局,可引入 UI 库如 Element P…

vue搜索功能实现

vue搜索功能实现

Vue搜索功能实现方法 在Vue中实现搜索功能可以通过多种方式完成,以下是几种常见的方法: 使用计算属性实现搜索 计算属性非常适合处理需要根据输入值动态过滤数据的情况。创建一个计算属性,根据搜索关键…

vue如何实现计算

vue如何实现计算

Vue 实现计算的方法 Vue 提供了多种方式来实现计算逻辑,包括计算属性、方法、侦听器等。以下是几种常见的实现方式: 计算属性(Computed Properties) 计算属性是基于它们的响应式…

vue实现吸附效果

vue实现吸附效果

Vue实现吸附效果的方法 吸附效果通常指页面滚动时,某个元素固定在特定位置(如顶部或底部)。以下是几种实现方式: 使用CSS的position: sticky 通过CSS的sticky定位实现吸附效…