当前位置:首页 > VUE

vue 实现轮播

2026-03-27 15:58:15VUE

使用 Swiper 插件实现轮播

安装 Swiper 依赖

npm install swiper

引入 Swiper 样式和组件

import { Swiper, SwiperSlide } from 'swiper/vue'
import 'swiper/css'

模板部分

<swiper
  :slides-per-view="3"
  :space-between="50"
  @swiper="onSwiper"
  @slideChange="onSlideChange"
>
  <swiper-slide>Slide 1</swiper-slide>
  <swiper-slide>Slide 2</swiper-slide>
  <swiper-slide>Slide 3</swiper-slide>
</swiper>

脚本部分

setup() {
  const onSwiper = (swiper) => {
    console.log(swiper)
  }
  const onSlideChange = () => {
    console.log('slide change')
  }
  return {
    onSwiper,
    onSlideChange,
  }
}

原生 Vue 实现简单轮播

数据定义

data() {
  return {
    currentIndex: 0,
    slides: [
      { id: 1, content: 'Slide 1' },
      { id: 2, content: 'Slide 2' },
      { id: 3, content: 'Slide 3' }
    ]
  }
}

模板结构

vue 实现轮播

<div class="carousel">
  <div class="slides" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
    <div class="slide" v-for="slide in slides" :key="slide.id">
      {{ slide.content }}
    </div>
  </div>
  <button @click="prev">Prev</button>
  <button @click="next">Next</button>
</div>

切换方法

methods: {
  prev() {
    this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
  },
  next() {
    this.currentIndex = (this.currentIndex + 1) % this.slides.length
  }
}

样式部分

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

自动轮播实现

添加自动轮播功能

vue 实现轮播

data() {
  return {
    timer: null,
    interval: 3000
  }
},
mounted() {
  this.startAutoPlay()
},
methods: {
  startAutoPlay() {
    this.timer = setInterval(() => {
      this.next()
    }, this.interval)
  },
  stopAutoPlay() {
    clearInterval(this.timer)
  }
}

添加鼠标悬停控制

<div class="carousel" @mouseenter="stopAutoPlay" @mouseleave="startAutoPlay">

添加指示器

模板添加指示器

<div class="indicators">
  <span 
    v-for="(slide, index) in slides" 
    :key="'indicator-'+slide.id"
    :class="{ active: currentIndex === index }"
    @click="goToSlide(index)"
  ></span>
</div>

跳转方法

methods: {
  goToSlide(index) {
    this.currentIndex = index
  }
}

指示器样式

.indicators {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}
.indicators span {
  width: 12px;
  height: 12px;
  margin: 0 5px;
  border-radius: 50%;
  background: #ccc;
  cursor: pointer;
}
.indicators span.active {
  background: #333;
}

标签: vue
分享给朋友:

相关文章

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-co…

vue 实现pdf

vue 实现pdf

在Vue中实现PDF功能 在Vue项目中实现PDF功能通常涉及PDF生成、预览或下载。以下是几种常见实现方式: 使用vue-pdf库预览PDF 安装依赖: npm install vue-pdf…

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

vue实现监控

vue实现监控

Vue 实现监控功能的方法 在 Vue 中实现监控功能通常涉及数据变化监听、事件监听或第三方监控工具集成。以下是几种常见实现方式: 使用 Vue 的 watch 监听数据变化 通过 watch 可以…

vue实现tab功能

vue实现tab功能

Vue 实现 Tab 功能的方法 使用动态组件和 v-if 指令 通过 v-if 或 v-show 控制不同 Tab 内容的显示与隐藏。这种方法适合简单的 Tab 切换需求。 <tem…

vue 实现的项目

vue 实现的项目

Vue 实现的项目类型 Vue.js 是一个渐进式 JavaScript 框架,适用于构建各种规模的项目。以下是一些常见的 Vue 实现项目类型: 单页应用 (SPA) Vue 非常适合构建单页应用…