当前位置:首页 > 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' }
    ]
  }
}

模板结构

<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%;
}

自动轮播实现

添加自动轮播功能

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

指示器样式

vue 实现轮播

.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 实现视频剪辑功能 在Vue中实现视频剪辑功能通常需要结合HTML5的<video>元素和第三方库(如FFmpeg.js或Video.js)。以下是几种常见的实现方法: 使用FFm…

vue拖拽实现

vue拖拽实现

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

vue实现list

vue实现list

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

vue实现增加

vue实现增加

实现 Vue 中的新增功能 在 Vue 中实现新增功能通常涉及数据绑定、表单处理和状态管理。以下是几种常见的实现方式: 数据绑定与列表渲染 通过 v-model 绑定表单输入,使用数组的 push…

vue实现文章滚动

vue实现文章滚动

Vue 实现文章滚动效果 使用 CSS 实现基础滚动 通过 CSS 的 overflow 和 height 属性控制滚动区域,适用于静态内容滚动。在 Vue 模板中定义固定高度的容器,内容超出后自动滚…

vue路由实现滑动

vue路由实现滑动

实现 Vue 路由滑动效果的方法 使用 Vue Router 结合 CSS 过渡动画 在 Vue Router 的路由视图组件 <router-view> 上添加过渡效果,结合 CSS 实…