首頁  >  文章  >  微信小程式  >  微信小程式 監聽手勢滑動切換頁面的實現

微信小程式 監聽手勢滑動切換頁面的實現

不言
不言原創
2018-06-23 15:04:445301瀏覽

這篇文章主要介紹了微信小程式監聽手勢滑動切換頁面實例詳解的相關資料,需要的朋友可以參考下

微信小程式監聽手勢滑動切換頁面實例詳解

1.對應的xml裡寫上手勢開始、滑動、結束的監聽:

<view class="touch" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd" ></view>

2.js:

var touchDot = 0;//触摸时的原点 
var time = 0;// 时间记录,用于滑动时且时间小于1s则执行左右滑动 
var interval = "";// 记录/清理时间记录 
Page({ 
 data: {...} 
   })

#
Page({ 
 data: { 
     ... 
 }, 
 // 触摸开始事件 
 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) { 
   wx.switchTab({ 
    url: &#39;../左滑页面/左滑页面&#39; 
   });  
  } 
  // 向右滑动 
  if (touchMove - touchDot >= 40 && time < 10) { 
   console.log(&#39;向右滑动&#39;); 
   wx.switchTab({ 
    url: &#39;../右滑页面/右滑页面&#39; 
   });  
  } 
 }, 
 // 触摸结束事件 
 touchEnd: function (e) { 
  clearInterval(interval); // 清除setInterval 
  time = 0; 
 }, 
. 
. 
. 
})

以上就是本文的全部內容,希望對大家的學習有幫助,更多相關內容請關注PHP中文網!

相關推薦:

微信小程序 scroll-view實作上拉載入與下拉刷新的實例

微信小程序 彈出視窗自訂的程式碼

微信小程式開發之跑步微信小程式

################################## #######

以上是微信小程式 監聽手勢滑動切換頁面的實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn