有些时候我们玩手机更喜欢使用手势滑动带来的用户操作体验。Vue touch directive是一个用于移动设备操作指令的轻量级的VUE组件。使用它可以轻松实现屏幕触控、滑动触发事件,提高用户体验。本文结合实例讲解如何实现Vue移动端右滑屏幕返回上一页。
安装依赖
使用npm安装vue-directive-touch组件。
引入组件
在main.js中引入vue-directive-touch。
使用
在App.vue的模板中加上滑动事件。
vue-directive-touch提供了以下事件类型:
单击: v-touch:tap
长按: v-touch:long
左滑: v-touch:left
右滑: v-touch:right
上滑: v-touch:up
下滑: v-touch:down
然后在script部分加上滑动事件方法。
接着我们在具体的页面写上逻辑跳转路由,注意具体页面设置好页面触控范围,让整个屏幕都可以touch。
参照今日头条app的图片栏目,我们还可以设置点击打开新页面,上滑打开评论窗口,下滑关闭图片等操作。
Vue touch directive项目地址:https://github.com/BensonDu/vue-directive-touch