当前位置:首页 > VUE

vue实现swipe

2026-01-13 00:14:40VUE

Vue 实现 Swipe 功能

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

安装 vue-awesome-swiper 库:

npm install swiper vue-awesome-swiper --save

在组件中引入并使用:

vue实现swipe

<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

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

vue实现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

注册插件并使用:

<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 中可以通过动态组件配合递归组件实现多级弹窗。定义一个基础弹窗组件,通过递归调用自身实现层级嵌套。 基础弹窗组件模板示例:…

vue表单实现搜索

vue表单实现搜索

实现Vue表单搜索功能 在Vue中实现表单搜索功能通常涉及表单绑定、事件处理和搜索逻辑。以下是具体实现方法: 数据绑定与表单结构 使用v-model实现表单与Vue数据的双向绑定: <tem…

vue实现下拉刷新组件

vue实现下拉刷新组件

实现下拉刷新组件的核心思路 下拉刷新功能通常通过监听触摸事件、滚动位置和动画效果实现。Vue中可以利用自定义指令或封装组件完成,以下为两种常见实现方式。 基于自定义指令的实现 创建指令监听触摸事件…

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue优秀实现

vue优秀实现

Vue.js 优秀实现方法 响应式数据管理 使用 Vue 的 ref 和 reactive 处理基础类型和对象类型数据,结合 computed 计算属性优化渲染性能。对于复杂状态,推荐 Pinia 替…

vue实现轮询

vue实现轮询

实现轮询的基本方法 在Vue中实现轮询可以通过setInterval或setTimeout配合递归调用完成。轮询通常用于定期向服务器请求数据更新。 使用setInterval的简单示例: da…