您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“小程序如何自定義索引菜單”,內容詳細,步驟清晰,細節處理妥當,希望這篇“小程序如何自定義索引菜單”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
<view class="indexes_chunk" wx:for="{{brandIndexList.brandGroupList}}" wx:key="item"> <view class="letter ancehor-{{item.indexLetter}}">{{item.indexLetter}}</view> <view class="choice" wx:for="{{item.brandList}}" wx:for-item="items" wx:key="items" wx:for-index="cindex"> <text class="text actives">{{items.enName}}{{items.cnName}}</text> <text class="iconfont icon-gouxuan"></text> </view> </view>
主要代碼:
function throttle(fn, interval) { var enterTime = 0;//觸發的時間 var gapTime = interval || 300 ;//間隔時間,如果interval不傳,則默認300ms return function() { var context = this; var backTime = new Date();//第一次函數return即觸發的時間 if (backTime - enterTime > gapTime) { fn.call(context,arguments); enterTime = backTime;//賦值給第一次觸發的時間,這樣就保存了第二次觸發的時間 } }; }; data:{ brandIndexList:{ brandGroupList:[ { brandList:[ {brandId:1, cnName: "愛馬仕A", enName: "Hermes", indexLetter: "A"} ], indexLetter: "A" }, { brandList:[ {brandId:2, cnName: "愛馬仕B", enName: "Hermesss", indexLetter: "B"} ], indexLetter: "B" }, { brandList:[ {brandId:3, cnName: "愛馬仕G", enName: "Hermes", indexLetter: "G"} ], indexLetter: "G" }, { brandList:[ {brandId:4, cnName: "愛馬仕M", enName: "Hermesss", indexLetter: "M"} ], indexLetter: "M" }, { brandList:[ {brandId:5, cnName: "愛馬仕P", enName: "Hermesss", indexLetter: "P"} ], indexLetter: "P" }, { brandList:[ {brandId:6, cnName: "愛馬仕V", enName: "Hermesss", indexLetter: "V"} ], indexLetter: "V" } ], indexLetterList: ["A", "B", "G", "M", "P", "V"] }, letterNodes:[],//索引菜單 全部節點位置 letterIndex:0, } onReady(){ let self = this; let indexLetterList = self.data.brandIndexList.indexLetterList, letterNodes = self.data.letterNodes, nodes = '.ancehor-', arrs = []; // 獲取所有索引錨點節點 indexLetterList.forEach((item)=>{ arrs.push(nodes+item); if(arrs.length == indexLetterList.length){ self.getDoms(arrs.join(','),(res)=>{ letterNodes = res.map((item)=>{return item.top-88}); self.setData({ letterNodes:letterNodes }); }); wx.hideLoading(); } }); }, // 動態獲取節點 getDoms(node,success){ let self = this, query = wx.createSelectorQuery(); setTimeout(()=>{ query.selectAll(node).boundingClientRect((res)=>{ success && success(res); }).exec() },1000); }, // 索引菜單點擊滾動 letterClick(e){ let self = this, index = e.currentTarget.dataset.index, letterNodes = self.data.letterNodes; wx.pageScrollTo({ scrollTop: letterNodes[index] }) }, // 頁面滾動 onPageScroll:throttle(function(e){ let self = this, scrollTop = e[0].scrollTop, stickyTop = self.data.stickyTop, letterNodes = self.data.letterNodes, showSticky = self.data.showSticky, letterIndex = self.data.letterIndex; // 顯示右側索引 if(scrollTop>=stickyTop){ showSticky = true; }else{ showSticky = false; } //滾動定位索引 letterNodes.forEach((item,index)=>{ if(scrollTop>=item){ letterIndex = index; // console.log(index) } }) // console.log(scrollTop) self.setData({ showSticky:showSticky, letterIndex:letterIndex }); },10)
讀到這里,這篇“小程序如何自定義索引菜單”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。