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

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

微信小程序的左右滑动触屏事件,主要有三个事件:touchstart,touchmove,touchend。
这三个事件最重要的属性是pageX和pageY,表示X,Y坐标。
touchstart在触摸开始时触发事件;
touchend在触摸结束时触发事件;
touchmove触摸的过程中不断激发这个事件;
这三个事件都有一个timeStamp的属性,查看timeStamp属性,可以看到顺序是touchstart => touchmove=> touchmove => ··· =>touchmove =>touchend。

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

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

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

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; // 回复滑动事件
},
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 13.1 事件流 “DOM2级事件”规定事件流包括3个阶段:事件捕获阶段,处于目标阶段,事件冒泡阶段。事件捕获表示...
    Elevens_regret阅读 3,362评论 0 0
  • 基于html5的移动web页面搭建技术总结 1.技术要点 1.1面向不同尺寸的手机屏幕,页面布局适配问题。 网页的...
    敲代码的张阅读 5,411评论 0 3
  • Web 浏览器中可能发生的事件有很多类型。如前所述,不同的事件类型具有不同的信息,而 DOM3 级事件规定了以下几...
    More_5897阅读 4,592评论 1 0
  • 原创微信公号:拍脑袋的发明 大部分情况下,一架永不落地的纸飞机需要这样的高级技能: 精准的设计,准确的折纸,恰到好...
    光区阅读 26,473评论 19 20
  • 本人承诺,文章内容为原创。 PART 1 古道旁,秋风四起,收缰停马,看骁骑旧营,枯槁遍地,昔日热血少年,如今提枪...
    流沙小妖怪阅读 5,019评论 34 27

友情链接更多精彩内容