vue 和vue-touch 实现移动端左右导航效果(仿京东移动站导航)

2020-06-16 05:38:27易采站长站整理

5、  Swipe事件:在指定的dom区域内,一个手指快速的在触屏上滑动。即我们平时用到最多的滑动事件。

Swipeleft:向左滑动、Swiperight:向右滑动、Swipeup:向上滑动、Swipedown:向下滑动

6、Tap事件:在指定的dom区域内,一个手指轻拍或点击时触发该事件(类似PC端的click)。该事件最大点击时间为250毫秒,如果超过250毫秒则按Press事件进行处理。

(以上作者毫无节奏的摘抄一下,其实我也是费好大劲搜索的呀)
这里注意swipe这个方法;我们不需要因为这个滑动和我们需要的滑动是不一样的;

Panstart 是我们需要的;

展示一下 div 代码层;


<v-touch class="ul"
v-on:pandown="onPanStart" //向下滑动
v-on:panmove="onPanmove" //滑动结束
v-on:panup="onPandup"> //向上滑动
</v-touch>

注意:也可以进行 事件的限制 用户多长时间可以执行


//我没用到这句
v-bind:pan-options="{ direction: 'panup', threshold: 100 }">

首先定一个 公共变量


//省去一些 代码
var lefthe = 0; //检测滑动的位置
onPanStart:function(data)
{
this.positionjson.transition=''
console.log(data)
var y = data.deltaY; //事件执行 所滑动的距离
lefthe = y + this.lefthe; //滑动的距离 记录到 lefthe 方便下次执行
console.log(lefthe)
if(lefthe >=50)
{
//限制 不能一直往上拉 拉到宇宙呢?我这里是写死的
lefthe = 0;
//回弹效果 在 滑动结束后 执行
return
}else{
}
this.positionjson.top = lefthe +'px';
},

滑动结束 事件


onPanend:function(data)
{
if(lefthe ==0)
{
console.log('现在是0')
this.positionjson.top = lefthe +'px';
//滑动结束 执行 一些事件 lefthe 0 的时候其实也就是下拉到顶的过程
this.positionjson.transition='0.2s ease 0s'
}else{
}
this.lefthe = lefthe;
},
//上拉代码块
onPandup:function(data)
{
var domul =document.getElementById('domul');
this.positionjson.transition=''
var y = data.deltaY; //下拉的距离
var boxheight = this.boxheight; //视图高度 也就是分辨率
var liheight = document.getElementById('libox').clientHeight; //左侧每个li的高度
console.log(liheight+'元素的高')
var zongheight = this.navapi.list.length * 70; //就能得出li父盒子高度
var bottomheight = -(zongheight - boxheight) + -100; //计算下拉到 滑动盒子的高度 - 视图高度 =上拉到底的位置