分享好友 编程语言首页 频道列表

微信小程序左右滑动切换页面示例代码--转载

小程序文章/教程  2023-02-09 09:560

微信小程序——左右滑动切换页面事件

微信小程序的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend。

这三个事件最重要的属性是pageX和pageY,表示X,Y坐标。

touchstart在触摸开始时触发事件;
touchend在触摸结束时触发事件;
touchmove触摸的过程中不断激发这个事件;

这三个事件都有一个timeStamp的属性,查看timeStamp属性,可以看到顺序是touchstart => touchmove=> touchmove => ··· =>touchmove =>touchend。

第一步:在wxml文件中绑定事件(需要左右滑动的界面)

1
2
3
<view class="container" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd">
 // do something
</view>
 

第二步:在js文件中处理左右滑动逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var touchDot = 0;//触摸时的原点
var time = 0;// 时间记录,用于滑动时且时间小于1s则执行左右滑动
var interval = "";// 记录/清理 时间记录
var nth = 0;// 设置活动菜单的index
var nthMax = 5;//活动菜单的最大个数
var tmpFlag = true;// 判断左右华东超出菜单最大值时不再执行滑动事件
 
// 触摸开始事件
touchStart:function(e){
  touchDot = e.touches[0].pageX; // 获取触摸时的原点
  // 使用js计时器记录时间 
  interval = setInterval(function(){
    time++;
  },100);
},
// 触摸移动事件
touchMove:function(e){
  var touchMove = e.touches[0].pageX;
  console.log("touchMove:"+touchMove+" touchDot:"+touchDot+" diff:"+(touchMove - touchDot));
  // 向左滑动 
  if(touchMove - touchDot <= -40 && time < 10){
    if(tmpFlag && nth < nthMax){ //每次移动中且滑动时不超过最大值 只执行一次
      var tmp = this.data.menu.map(function (arr, index) {
        tmpFlag = false;
        if(arr.active){ // 当前的状态更改
          nth = index;
          ++nth;
          arr.active = nth > nthMax ? true : false;
        }
        if(nth == index){ // 下一个的状态更改
          arr.active = true;
          name = arr.value;
        }
        return arr;
      })
      this.getNews(name); // 获取新闻列表
      this.setData({menu : tmp}); // 更新菜单
    }
  }
  // 向右滑动
  if(touchMove - touchDot >= 40 && time < 10){
    if(tmpFlag && nth > 0){
      nth = --nth < 0 ? 0 : nth;
      var tmp = this.data.menu.map(function (arr, index) {
        tmpFlag = false;
        arr.active = false;
        // 上一个的状态更改
        if(nth == index){
          arr.active = true;
          name = arr.value;
        }
        return arr;
      })
      this.getNews(name); // 获取新闻列表
      this.setData({menu : tmp}); // 更新菜单
    }
  }
  // touchDot = touchMove; //每移动一次把上一次的点作为原点(好像没啥用)
},
 // 触摸结束事件
touchEnd:function(e){
  clearInterval(interval); // 清除setInterval
  time = 0;
  tmpFlag = true; // 回复滑动事件

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://www.jb51.net/article/107020.htm

查看更多关于【小程序文章/教程】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
小程序 AI/AR 能力
一、关于 VisionKit1、定义VisionKit 为小程序提供了开发 AR 功能的能力,包含了 AR 在内的视觉算法。2、版本提供了 V1 和 V2 两个版本,区别如下:V1平面接口,适用于用户在平面场景下,例如桌面,地面,泛平面场景,放置虚拟物体,不提供真实世界距离。用户

0评论2023-03-08842

Python小程序——快排算法 快排 python
1 def Partition(list,p,q): 2 #这里是用来分块的算法。 3 x = list[p] 4 i = p 5 for j in range(p+1,q+1): #注意range是顾前不顾后的,所以后面的区间值要大一位 6 if list[j]x: 7 i+=1 8 list[i],list[j] = list[j],list[i] 9 10 list[p], list[i] = list[

0评论2023-02-09351

c++第一个小程序 第一个小程序是什么
 #include iostreamusing namespace std;int main(){const int SIZE=50;//定义大小。char name[SIZE]; cout"please input you name!\n"; //提示cinname;//输入cout"hello world:"nameendl; //输出return 0;}   #include iostreamusing namespace std;int m

0评论2023-02-09579

微信小程序 错误记录
1、报错this.getUserInfo(this.setData) is not a function;at pages/index/index onShow function;at api request success callback functionTypeError: this.getUserInfo is not a function在回调结果里调用这个页面的函数 this.fun() 或者 this.setData 时

0评论2023-02-09477

【小程序】添加tabBar后navigateTo失效
某页面.js//事件处理函数bindViewTap() {wx.navigateTo({url: '../logs/logs',})}, app.json"tabBar": {"backgroundColor": "black","color":"white","list": [{"pagePath": "pages/index/inde

0评论2023-02-09474

小程序组件之间的通信 小程序子父子组件通信
前言:其实之前就想写这个的,因为我觉得这么模块化的框架,组件之间通信是非常重要的,也是最经常用到的一块儿,只是之前在项目里一直没用到跨组件通信,现在用到了,也会用了,就一起写出来得了 :) 一、父、子组件之间的通信注:首先我们先将子组件在父组

0评论2023-02-09452

让vue用于小程序setData方法
setData:function(obj){let that = this;let keys = [];let val,data;Object.keys(obj).forEach(function(key){keys = key.split('.');val = obj[key];data = that.$data;keys.forEach(function(key2,index){if(index+1 == keys.length){that.$set(data,key2,

0评论2023-02-09891

微信小程序 canvas导出图片模糊
//保存到手机相册save:function () {wx.canvasToTempFilePath({x: 0,y: 0,width: 375, //导出图片的宽height: 680, //导出图片的高destWidth: 375 * 750 / wx.getSystemInfoSync().windowWidth, //绘制canvas的时候用的是px, 这里换算成rpx ,导出后非常清晰

0评论2023-02-09339

微信小程序hidden问题 微信小程序隐藏view
    context.fillText('Hello World', 20, 380);                wx.drawCanvas({                    canvasId: '2',                    actions: context.getActions()                });       

0评论2023-02-09905

小程序***滑动的表格 小程序实现左右滑动
// pages/test/test.jsPage({/** * 页面的初始数据 */data: {headerList: [{name: '表头一',number: 'A201',type: "标准间"}, {name: '表头二',number: 'A202',type: "大床"}, {name: '表头三',number: 'A203',t

0评论2023-02-09530

微信小程序实现图片双滑缩放大小 微信小程序图片点击放大
在做小程序开发的过程中,后端传来一张图片地图,需要实现双手指滑动,使图片缩放,最终得出了一下代码:  js :Page({data: {touch: {distance: 0,scale: 1,baseWidth: null,baseHeight: null,scaleWidth: null,scaleHeight: null}},touchStartHandle(e) {/

0评论2023-02-09697

更多推荐