当前位置:首页 > 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 状态管理实现方法 使用 Vuex(官方推荐) Vuex 是 Vue 的官方状态管理库,适合中大型应用。 安装 Vuex: npm install vuex --save 创建 Store 示…

vue实现右下角弹框

vue实现右下角弹框

实现右下角弹框的基本思路 在Vue中实现右下角弹框,通常需要结合CSS定位和Vue的组件化特性。弹框可以通过绝对定位固定在右下角,并通过Vue控制其显示与隐藏。 创建弹框组件 新建一个Vue组件(如…

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.def…

vue实现通讯

vue实现通讯

Vue 组件通讯方法 父子组件通讯 父组件向子组件传递数据通过 props,子组件向父组件传递数据通过 $emit 事件。 父组件模板: <child-component :message=…

vue filter 实现

vue filter 实现

在 Vue.js 中,过滤器(Filter)是一种用于文本格式化的功能,可以在模板中直接使用。以下是实现过滤器的详细方法: 全局过滤器注册 通过 Vue.filter 方法注册全局过滤器,可在所有组…

自实现vue

自实现vue

实现 Vue 的核心功能 要实现一个简化版的 Vue,需要理解其核心功能:数据响应式、模板编译、虚拟 DOM 和组件系统。以下是关键步骤的分解: 数据响应式 Vue 使用 Object.define…