- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) >
- 微信小程序中如何實(shí)現左右滑動(dòng)切換頁(yè)面功能
這篇文章主要為大家展示了“微信小程序中如何實(shí)現左右滑動(dòng)切換頁(yè)面功能”,內容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習一下“微信小程序中如何實(shí)現左右滑動(dòng)切換頁(yè)面功能”這篇文章吧。
微信小程序——左右滑動(dòng)切換頁(yè)面事件
微信小程序的左右滑動(dòng)觸屏事件,主要有三個(gè)事件:touchstart,touchmove,touchend。
這三個(gè)事件最重要的屬性是pageX和pageY,表示X,Y坐標。
touchstart在觸摸開(kāi)始時(shí)觸發(fā)事件;
touchend在觸摸結束時(shí)觸發(fā)事件;
touchmove觸摸的過(guò)程中不斷激發(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則執行左右滑動(dòng) var interval = "";// 記錄/清理 時(shí)間記錄 var nth = 0;// 設置活動(dòng)菜單的index var nthMax = 5;//活動(dòng)菜單的最大個(gè)數 var tmpFlag = true;// 判斷左右華東超出菜單最大值時(shí)不再執行滑動(dòng)事件 // 觸摸開(kāi)始事件 touchStart:function(e){ touchDot = e.touches[0].pageX; // 獲取觸摸時(shí)的原點(diǎn) // 使用js計時(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í)不超過(guò)最大值 只執行一次 var tmp = this.data.menu.map(function (arr, index) { tmpFlag = false; if(arr.active){ // 當前的狀態(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)(好像沒(méi)啥用) }, // 觸摸結束事件 touchEnd:function(e){ clearInterval(interval); // 清除setInterval time = 0; tmpFlag = true; // 回復滑動(dòng)事件 },
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系站長(cháng)郵箱:ts@56dr.com進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2 建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站