当前位置:首页 > 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 3 实现的简单登录功能示例,包含表单验证和基础交互逻辑: 创建 Vue 组件 <template> <div class="login-contain…

vue实现密码框的包

vue实现密码框的包

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

vue 实现jqslidedown

vue 实现jqslidedown

在 Vue 中实现类似 jQuery 的 slideDown 效果,可以通过 Vue 的过渡系统或 CSS 动画结合动态样式绑定来实现。以下是几种实现方式: 使用 Vue Transition…

vue 实现豆瓣

vue 实现豆瓣

以下是基于 Vue 实现豆瓣电影类功能的实现方案,涵盖核心模块和技术要点: 数据获取与 API 调用 使用豆瓣开放 API(需注意调用频率限制)或第三方代理接口 推荐 axios 进行异步请求,配合…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json)…

vue实现分发

vue实现分发

Vue 实现分发的方法 在 Vue 中,分发(Distribution)通常指的是将内容从父组件传递到子组件,或者通过插槽(Slots)机制让父组件控制子组件的部分渲染内容。以下是几种常见的实现分发的…