当前位置:首页 > VUE

轮播图vue实现

2026-02-11 09:33:28VUE

使用 Vue 实现轮播图

基础轮播图实现

安装依赖(如需要):

npm install vue-awesome-swiper

引入 Swiper 组件:

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

模板部分:

<swiper>
  <swiper-slide>Slide 1</swiper-slide>
  <swiper-slide>Slide 2</swiper-slide>
  <swiper-slide>Slide 3</swiper-slide>
</swiper>

自定义配置选项

添加导航和分页:

const swiperOptions = {
  navigation: true,
  pagination: { clickable: true },
  loop: true,
  autoplay: {
    delay: 3000,
    disableOnInteraction: false
  }
}

模板绑定配置:

<swiper :modules="modules" :options="swiperOptions">
  <!-- slides -->
</swiper>

纯 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="slideStyle">
    <div v-for="slide in slides" :key="slide.id" class="slide">
      {{ slide.content }}
    </div>
  </div>
  <button @click="prev">Prev</button>
  <button @click="next">Next</button>
</div>

计算属性和方法:

computed: {
  slideStyle() {
    return {
      transform: `translateX(-${this.currentIndex * 100}%)`
    }
  }
},
methods: {
  next() {
    this.currentIndex = (this.currentIndex + 1) % this.slides.length
  },
  prev() {
    this.currentIndex = (this.currentIndex - 1 + this.slides.length) % this.slides.length
  }
}

过渡动画效果

添加 CSS 过渡:

.slides {
  display: flex;
  transition: transform 0.5s ease;
}
.slide {
  flex: 0 0 100%;
}

自动轮播功能

添加自动轮播:

mounted() {
  this.autoPlay = setInterval(this.next, 3000)
},
beforeDestroy() {
  clearInterval(this.autoPlay)
}

响应式处理

监听窗口变化:

data() {
  return {
    windowWidth: window.innerWidth
  }
},
mounted() {
  window.addEventListener('resize', this.handleResize)
},
methods: {
  handleResize() {
    this.windowWidth = window.innerWidth
  }
}

触摸支持

添加触摸事件:

<div 
  class="slides"
  @touchstart="touchStart"
  @touchmove="touchMove"
  @touchend="touchEnd"
>

触摸方法实现:

轮播图vue实现

data() {
  return {
    touchStartX: 0,
    touchEndX: 0
  }
},
methods: {
  touchStart(e) {
    this.touchStartX = e.changedTouches[0].screenX
  },
  touchEnd(e) {
    this.touchEndX = e.changedTouches[0].screenX
    this.handleSwipe()
  },
  handleSwipe() {
    if (this.touchEndX < this.touchStartX) this.next()
    if (this.touchEndX > this.touchStartX) this.prev()
  }
}

标签: 轮播图vue
分享给朋友:

相关文章

实现 vue 框架

实现 vue 框架

Vue 框架的基本实现 Vue 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现 Vue 框架的基本步骤和核心概念。 核心概念 响应式数据绑定 Vue 的核心特性之一是响应式数…

vue实现异步

vue实现异步

Vue 实现异步操作的常见方法 Vue 提供了多种方式来处理异步操作,包括数据获取、事件处理等场景。 使用 async/await 在 Vue 方法中可以直接使用 async/await 语法处理异…

vue实现定位

vue实现定位

Vue 中实现定位的方法 在 Vue 中实现定位可以通过以下几种方式: 使用 CSS 定位 通过 CSS 的 position 属性实现定位,Vue 中可以直接在组件的 <style>…

vue滚动实现

vue滚动实现

Vue 滚动实现方法 使用原生滚动 在 Vue 中可以通过 ref 获取 DOM 元素,调用原生滚动方法实现滚动效果。适用于简单场景。 <template> <div ref=…

vue实现123456

vue实现123456

Vue 实现数字 123456 的显示 在 Vue 中显示数字 123456 非常简单,可以通过以下方法实现: 方法一:直接在模板中显示 <template> <div>…

vue实现路径

vue实现路径

Vue 实现路径的方法 在 Vue 中实现路径管理通常涉及 Vue Router 的使用,以下是一些常见的实现方法: 安装 Vue Router 通过 npm 或 yarn 安装 Vue Route…