当前位置:首页 > VUE

vue实现swipe

2026-02-10 07:33:00VUE

Vue 实现 Swipe 功能

在 Vue 中实现 Swipe(滑动)功能可以通过多种方式实现,包括使用原生事件、第三方库或自定义指令。以下是几种常见的方法:

使用原生事件监听

通过监听 touchstarttouchmovetouchend 事件,可以实现基础的滑动功能。

<template>
  <div 
    @touchstart="handleTouchStart"
    @touchmove="handleTouchMove"
    @touchend="handleTouchEnd"
    class="swipe-container"
  >
    <div class="content">滑动内容</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      startX: 0,
      endX: 0,
    };
  },
  methods: {
    handleTouchStart(e) {
      this.startX = e.touches[0].clientX;
    },
    handleTouchMove(e) {
      this.endX = e.touches[0].clientX;
    },
    handleTouchEnd() {
      if (this.startX - this.endX > 50) {
        console.log('向左滑动');
      } else if (this.endX - this.startX > 50) {
        console.log('向右滑动');
      }
    },
  },
};
</script>

<style>
.swipe-container {
  width: 100%;
  height: 200px;
  background: #f0f0f0;
  overflow: hidden;
}
</style>

使用第三方库(如 vue-swipeswiper

swiper 是一个流行的滑动库,支持 Vue 集成,功能强大且易于使用。

安装 swiper

vue实现swipe

npm install swiper

在 Vue 中使用 swiper

<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide">Slide 1</div>
      <div class="swiper-slide">Slide 2</div>
      <div class="swiper-slide">Slide 3</div>
    </div>
  </div>
</template>

<script>
import Swiper from 'swiper';
import 'swiper/swiper-bundle.css';

export default {
  mounted() {
    new Swiper('.swiper-container', {
      loop: true,
      autoplay: {
        delay: 3000,
      },
    });
  },
};
</script>

<style>
.swiper-container {
  width: 100%;
  height: 300px;
}
.swiper-slide {
  background: #ccc;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

使用 Vue 指令实现自定义滑动

可以通过自定义指令封装滑动逻辑,方便复用。

vue实现swipe

<template>
  <div v-swipe="handleSwipe" class="swipe-area">
    滑动区域
  </div>
</template>

<script>
export default {
  directives: {
    swipe: {
      bind(el, binding) {
        let startX = 0;
        let endX = 0;

        el.addEventListener('touchstart', (e) => {
          startX = e.touches[0].clientX;
        });

        el.addEventListener('touchmove', (e) => {
          endX = e.touches[0].clientX;
        });

        el.addEventListener('touchend', () => {
          if (startX - endX > 50) {
            binding.value('left');
          } else if (endX - startX > 50) {
            binding.value('right');
          }
        });
      },
    },
  },
  methods: {
    handleSwipe(direction) {
      console.log(`滑动方向:${direction}`);
    },
  },
};
</script>

<style>
.swipe-area {
  width: 100%;
  height: 200px;
  background: #e0e0e0;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

使用 vue-touch

vue-touch 是一个专门为 Vue 设计的触摸事件库,支持滑动、点击等手势。

安装 vue-touch

npm install vue-touch@next

在 Vue 中使用 vue-touch

<template>
  <v-touch @swipeleft="onSwipeLeft" @swiperight="onSwipeRight">
    <div class="swipe-box">滑动区域</div>
  </v-touch>
</template>

<script>
import { VueTouch } from 'vue-touch';

export default {
  components: {
    'v-touch': VueTouch,
  },
  methods: {
    onSwipeLeft() {
      console.log('向左滑动');
    },
    onSwipeRight() {
      console.log('向右滑动');
    },
  },
};
</script>

<style>
.swipe-box {
  width: 100%;
  height: 200px;
  background: #ddd;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

总结

  • 原生事件:适合简单滑动需求,但需要手动处理事件逻辑。
  • 第三方库(如 swiper:功能强大,适合复杂滑动场景(如轮播图)。
  • 自定义指令:可复用性强,适合封装通用滑动逻辑。
  • vue-touch:专门为 Vue 设计,简化手势事件处理。

标签: vueswipe
分享给朋友:

相关文章

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…

vue 实现

vue 实现

Vue 实现的基本步骤 安装 Vue.js 可以通过 npm 或 CDN 引入。使用 npm 安装适合大型项目,CDN 适合快速原型开发。 npm install vue 在 HTML 文件中引入…

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &l…

vue 实现关闭

vue 实现关闭

Vue 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件:…

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <div…