当前位置:首页 > VUE

vue实现swipe

2026-01-13 00:14:40VUE

Vue 实现 Swipe 功能

使用第三方库(如 vue-awesome-swiper)

安装 vue-awesome-swiper 库:

npm install swiper vue-awesome-swiper --save

在组件中引入并使用:

<template>
  <swiper :options="swiperOption">
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
    <swiper-slide>Slide 3</swiper-slide>
  </swiper>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      swiperOption: {
        autoplay: true,
        loop: true
      }
    }
  }
}
</script>

自定义实现 Swipe

监听触摸事件实现基本滑动功能:

<template>
  <div 
    class="swipe-container"
    @touchstart="onTouchStart"
    @touchmove="onTouchMove"
    @touchend="onTouchEnd"
  >
    <div class="swipe-content" :style="contentStyle">
      <div v-for="(item, index) in items" :key="index" class="slide">
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['Slide 1', 'Slide 2', 'Slide 3'],
      startX: 0,
      currentX: 0,
      isDragging: false,
      currentIndex: 0
    }
  },
  computed: {
    contentStyle() {
      return {
        transform: `translateX(${-this.currentIndex * 100 + this.currentX}px)`,
        transition: this.isDragging ? 'none' : 'transform 0.3s ease'
      }
    }
  },
  methods: {
    onTouchStart(e) {
      this.startX = e.touches[0].clientX
      this.isDragging = true
    },
    onTouchMove(e) {
      if (!this.isDragging) return
      this.currentX = e.touches[0].clientX - this.startX
    },
    onTouchEnd() {
      this.isDragging = false
      const threshold = 50

      if (this.currentX > threshold && this.currentIndex > 0) {
        this.currentIndex--
      } else if (this.currentX < -threshold && this.currentIndex < this.items.length - 1) {
        this.currentIndex++
      }

      this.currentX = 0
    }
  }
}
</script>

<style>
.swipe-container {
  overflow: hidden;
  position: relative;
  width: 100%;
}

.swipe-content {
  display: flex;
  width: 100%;
}

.slide {
  flex: 0 0 100%;
  min-height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #eee;
  border: 1px solid #ddd;
}
</style>

使用 Vue 手势库(如 vue-touch)

安装 vue-touch:

npm install vue-touch@next --save

注册插件并使用:

vue实现swipe

<template>
  <v-touch @swipeleft="onSwipeLeft" @swiperight="onSwipeRight">
    <div class="swipe-content">
      <div v-show="currentIndex === 0">Slide 1</div>
      <div v-show="currentIndex === 1">Slide 2</div>
      <div v-show="currentIndex === 2">Slide 3</div>
    </div>
  </v-touch>
</template>

<script>
import VueTouch from 'vue-touch'
import Vue from 'vue'

Vue.use(VueTouch)

export default {
  data() {
    return {
      currentIndex: 0
    }
  },
  methods: {
    onSwipeLeft() {
      if (this.currentIndex < 2) {
        this.currentIndex++
      }
    },
    onSwipeRight() {
      if (this.currentIndex > 0) {
        this.currentIndex--
      }
    }
  }
}
</script>

标签: vueswipe
分享给朋友:

相关文章

vue实现后退

vue实现后退

Vue 实现后退功能的方法 在 Vue 中实现后退功能通常可以通过以下几种方式完成,具体取决于应用场景和需求。 使用 window.history API 通过原生 JavaScript 的 win…

vue实现 弹窗

vue实现 弹窗

Vue 实现弹窗的基本方法 使用 Vue 实现弹窗可以通过多种方式,包括组件化、指令、插件等。以下是几种常见的实现方法。 组件化实现弹窗 创建一个独立的弹窗组件,通过 props 控制显示与隐藏。…

vue滑块实现

vue滑块实现

Vue滑块实现方法 使用原生HTML5 range input Vue中可以绑定原生HTML5的range类型input元素实现基础滑块功能: <template> <div&…

自己实现vue

自己实现vue

实现简易版 Vue.js 核心功能 要实现一个简易版 Vue.js,需要理解其核心功能:数据响应式、模板编译、依赖收集和虚拟 DOM。以下分模块实现关键功能。 数据响应式(Reactivity) 通…

vue如何实现计算

vue如何实现计算

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

vue实现div单选

vue实现div单选

Vue 实现 div 单选 在 Vue 中实现 div 单选可以通过 v-model 结合 v-for 和事件绑定来完成。以下是具体实现方法: 模板部分 <template>…