当前位置:首页 > 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"
>

触摸方法实现:

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中实现手写签名功能,通常需要借助HTML5的Canvas API来捕获用户的绘制操作。核心流程包括初始化画布、监听触摸/鼠标事件、记录轨迹以及生成签名图片。 安装依…

vue实现旋转

vue实现旋转

Vue 实现旋转效果的方法 在 Vue 中实现旋转效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画 通过 Vue 的 :class 或 :style 绑定 CSS 动画实现旋转效…

vue实现$.extend

vue实现$.extend

Vue 实现类似 jQuery 的 $.extend 功能 在 Vue 中实现类似 jQuery 的 $.extend 功能,可以通过多种方式完成。$.extend 主要用于合并多个对象的属性,Vue…

vue的实现

vue的实现

Vue 的实现原理 Vue 是一个渐进式 JavaScript 框架,其核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是 Vue 实现的主要技术细节。 响应式系统 Vue 的响…

tabbar 实现 vue

tabbar 实现 vue

在 Vue 中实现 TabBar 功能可以通过多种方式完成,以下是几种常见的方法: 使用 Vue Router 结合自定义组件 创建一个自定义的 TabBar 组件,结合 Vue Router 实…

vue实现定时

vue实现定时

Vue 实现定时功能的方法 使用 setInterval 和 clearInterval 在 Vue 中可以通过 setInterval 和 clearInterval 实现定时功能。在组件的 mou…