小程序自定義索引菜單
更新時(shí)間:2022年07月17日 11:47:53 作者:摯友陽光
這篇文章主要為大家詳細(xì)介紹了小程序自定義索引菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了小程序自定義索引菜單的具體代碼,供大家參考,具體內(nèi)容如下

<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;//觸發(fā)的時(shí)間
? ? var gapTime = interval || 300 ;//間隔時(shí)間,如果interval不傳,則默認(rèn)300ms
? ? return function() {
? ? ? var context = this;
? ? ? var backTime = new Date();//第一次函數(shù)return即觸發(fā)的時(shí)間
? ? ? if (backTime - enterTime > gapTime) {
? ? ? ? fn.call(context,arguments);
? ? ? ? enterTime = backTime;//賦值給第一次觸發(fā)的時(shí)間,這樣就保存了第二次觸發(fā)的時(shí)間
? ? ? }
? ? };
};
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:[],//索引菜單 全部節(jié)點(diǎn)位置
? ?letterIndex:0,
}
? onReady(){
? ? ?
? ? ? ? let self = this;
? ? ? ? let indexLetterList = self.data.brandIndexList.indexLetterList,
? ? ? ? ? ? letterNodes = self.data.letterNodes,
? ? ? ? ? ? nodes = '.ancehor-',
? ? ? ? ? ? arrs = [];
? ? ? ? ? ? // 獲取所有索引錨點(diǎn)節(jié)點(diǎn)
? ? ? ? ? ? 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();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? },
? ? // 動(dòng)態(tài)獲取節(jié)點(diǎn)
? ? ?getDoms(node,success){
? ? ? ? let self = this,
? ? ? ? ? ? query = wx.createSelectorQuery();
? ? ? ? ? ? setTimeout(()=>{
? ? ? ? ? ? ? ? query.selectAll(node).boundingClientRect((res)=>{
? ? ? ? ? ? ? ? ? ? success && success(res);
? ? ? ? ? ? ? ? }).exec()
? ? ? ? ? ? },1000);
? ? },
? ? ?// 索引菜單點(diǎn)擊滾動(dòng)
? ? letterClick(e){
? ? ? ? let self = this,
? ? ? ? ? ? index = e.currentTarget.dataset.index,
? ? ? ? ? ? letterNodes = self.data.letterNodes;
? ? ? ? ? ? wx.pageScrollTo({
? ? ? ? ? ? ? ? scrollTop: letterNodes[index]
? ? ? ? ? ? })
? ? },
? ? ?// 頁面滾動(dòng)
? ? 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;
? ? ? ? // 顯示右側(cè)索引
? ? ? ? if(scrollTop>=stickyTop){
? ? ? ? ? ? showSticky = true;
? ? ? ? }else{
? ? ? ? ? ? showSticky = false;
? ? ? ? }
? ? ? ? //滾動(dòng)定位索引
? ? ? ? letterNodes.forEach((item,index)=>{
? ? ? ? ? ? if(scrollTop>=item){
? ? ? ? ? ? ? ? letterIndex = index;
? ? ? ? ? ? ? ? // console.log(index)
? ? ? ? ? ? }
? ? ? ? })
? ? ??
? ? ? ? // console.log(scrollTop)
? ? ? ? self.setData({
? ? ? ? ? ? showSticky:showSticky,
? ? ? ? ? ? letterIndex:letterIndex
? ? ? ? });
? ? },10)以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript function調(diào)用時(shí)的參數(shù)檢測(cè)常用辦法
js中并不直接支持類似c#的方法重載,所以只能變相的來解決,示意代碼:(利用了內(nèi)置屬性arguments)2010-02-02
詳解JavaScript中的強(qiáng)制類型轉(zhuǎn)換和自動(dòng)類型轉(zhuǎn)換
這篇文章中我們將深入探索一下 JavaScript 中的類型轉(zhuǎn)換,揭示強(qiáng)制類型轉(zhuǎn)換、自動(dòng)類型轉(zhuǎn)換和轉(zhuǎn)換函數(shù)的奧秘,快跟隨小編一起學(xué)習(xí)一下吧2023-12-12
微信小程序 如何獲取網(wǎng)絡(luò)狀態(tài)
這篇文章主要介紹了微信小程序 如何獲取網(wǎng)絡(luò)狀態(tài),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
淺談一個(gè)webpack構(gòu)建速度優(yōu)化誤區(qū)
這篇文章主要介紹了淺談一個(gè)webpack構(gòu)建速度優(yōu)化誤區(qū),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06
基于cornerstone.js的dicom醫(yī)學(xué)影像查看瀏覽功能
這篇文章主要介紹了基于cornerstone.js的dicom醫(yī)學(xué)影像查看瀏覽功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07

