温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

小程序如何自定义索引菜单

发布时间:2022-07-18 09:38:39 来源:亿速云 阅读:204 作者:iii 栏目:开发技术

本文小编为大家详细介绍“小程序如何自定义索引菜单”,内容详细,步骤清晰,细节处理妥当,希望这篇“小程序如何自定义索引菜单”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

小程序如何自定义索引菜单

<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)

读到这里,这篇“小程序如何自定义索引菜单”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI