微信小程序左右滑動(dòng)切換頁面詳解及實(shí)例代碼
微信小程序——左右滑動(dòng)切換頁面事件
微信小程序的左右滑動(dòng)觸屏事件,主要有三個(gè)事件:touchstart,touchmove,touchend。
這三個(gè)事件最重要的屬性是pageX和pageY,表示X,Y坐標(biāo)。
touchstart在觸摸開始時(shí)觸發(fā)事件;
touchend在觸摸結(jié)束時(shí)觸發(fā)事件;
touchmove觸摸的過程中不斷激發(fā)這個(gè)事件;
這三個(gè)事件都有一個(gè)timeStamp的屬性,查看timeStamp屬性,可以看到順序是touchstart => touchmove=> touchmove => ··· =>touchmove =>touchend。
第一步:在wxml文件中綁定事件(需要左右滑動(dòng)的界面)
<view class="container" bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd"> // do something </view>
第二步:在js文件中處理左右滑動(dòng)邏輯
var touchDot = 0;//觸摸時(shí)的原點(diǎn)
var time = 0;// 時(shí)間記錄,用于滑動(dòng)時(shí)且時(shí)間小于1s則執(zhí)行左右滑動(dòng)
var interval = "";// 記錄/清理 時(shí)間記錄
var nth = 0;// 設(shè)置活動(dòng)菜單的index
var nthMax = 5;//活動(dòng)菜單的最大個(gè)數(shù)
var tmpFlag = true;// 判斷左右華東超出菜單最大值時(shí)不再執(zhí)行滑動(dòng)事件
// 觸摸開始事件
touchStart:function(e){
touchDot = e.touches[0].pageX; // 獲取觸摸時(shí)的原點(diǎn)
// 使用js計(jì)時(shí)器記錄時(shí)間
interval = setInterval(function(){
time++;
},100);
},
// 觸摸移動(dòng)事件
touchMove:function(e){
var touchMove = e.touches[0].pageX;
console.log("touchMove:"+touchMove+" touchDot:"+touchDot+" diff:"+(touchMove - touchDot));
// 向左滑動(dòng)
if(touchMove - touchDot <= -40 && time < 10){
if(tmpFlag && nth < nthMax){ //每次移動(dòng)中且滑動(dòng)時(shí)不超過最大值 只執(zhí)行一次
var tmp = this.data.menu.map(function (arr, index) {
tmpFlag = false;
if(arr.active){ // 當(dāng)前的狀態(tài)更改
nth = index;
++nth;
arr.active = nth > nthMax ? true : false;
}
if(nth == index){ // 下一個(gè)的狀態(tài)更改
arr.active = true;
name = arr.value;
}
return arr;
})
this.getNews(name); // 獲取新聞列表
this.setData({menu : tmp}); // 更新菜單
}
}
// 向右滑動(dòng)
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;
// 上一個(gè)的狀態(tài)更改
if(nth == index){
arr.active = true;
name = arr.value;
}
return arr;
})
this.getNews(name); // 獲取新聞列表
this.setData({menu : tmp}); // 更新菜單
}
}
// touchDot = touchMove; //每移動(dòng)一次把上一次的點(diǎn)作為原點(diǎn)(好像沒啥用)
},
// 觸摸結(jié)束事件
touchEnd:function(e){
clearInterval(interval); // 清除setInterval
time = 0;
tmpFlag = true; // 回復(fù)滑動(dòng)事件
},
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- 微信小程序MUI側(cè)滑導(dǎo)航菜單示例(Popup彈出式,左側(cè)滑動(dòng),右側(cè)不動(dòng))
- 微信小程序MUI側(cè)滑導(dǎo)航菜單示例(Popup彈出式,左側(cè)不動(dòng),右側(cè)滑動(dòng))
- 微信小程序左滑動(dòng)顯示菜單功能的實(shí)現(xiàn)
- 微信小程序圖片橫向左右滑動(dòng)案例
- 微信小程序側(cè)邊欄滑動(dòng)特效(左右滑動(dòng))
- 微信小程序左右滑動(dòng)的實(shí)現(xiàn)代碼
- 微信小程序 向左滑動(dòng)刪除功能的實(shí)現(xiàn)
- 微信小程序滾動(dòng)Tab實(shí)現(xiàn)左右可滑動(dòng)切換
- 微信小程序?qū)崿F(xiàn)左側(cè)滑動(dòng)導(dǎo)航欄
- 微信小程序?qū)崿F(xiàn)點(diǎn)餐小程序左側(cè)滑動(dòng)菜單
相關(guān)文章
沒有resolve及reject的Promise是否會造成內(nèi)存泄露
這篇文章主要為大家介紹了一直沒有resolve及reject的Promise是否會造成內(nèi)存泄露的問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
pnpm?tauri?build?默認(rèn)com.tauri.dev打包報(bào)錯(cuò)解決
這篇文章主要介紹了pnpm?tauri?build?默認(rèn)com.tauri.dev打包報(bào)錯(cuò)解決方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
微信小程序 input輸入框控件詳解及實(shí)例(多種示例)
這篇文章主要介紹了微信小程序 input輸入框控件詳解及實(shí)例(多種示例)的相關(guān)資料,輸入框在程序中是最常見的,登錄,注冊,獲取搜索框中的內(nèi)容等等都需要,需要的朋友可以參考下2016-12-12
微信小程序 輪播圖swiper詳解及實(shí)例(源碼下載)
這篇文章主要介紹了微信小程序 輪播圖swiper詳解及實(shí)例(源碼下載)的相關(guān)資料,實(shí)用小技巧自定義輪播圖swiper dots默認(rèn)樣式,需要的朋友可以參考下2017-01-01
微信小程序 簡單實(shí)例(閱讀器)的實(shí)例開發(fā)
這篇文章主要介紹了微信小程序 簡單實(shí)例(閱讀器)的實(shí)例開發(fā)的相關(guān)資料,需要的朋友可以參考下2016-09-09

