小程序实现tab标签页,并且每个标签页独立实现加载更多(上拉触底)

原理:如果是普通页面的上拉触底事件可用小程序提供的:onReachBottom函数;

但现在是tab页,每个页面要独立进行滚动加载,这里用到了scroll-view的bindscrolltolower函数

 

注意:有些童鞋自己写完之后bindscrolltolower不起作用,要注意几个点:scroll-view的高度设置没有设置,或者直接设置成100%,没有设置scroll-y="true"。

我这里也设置了100%就有效呢?因为我在它的父元素已经给了一定的高度,它在父元素里面100%才有效,而且滚动的时候也不会覆盖tab标签页头。也可以把高度设置成固定值:600px这种的,看个人需要。

 

效果图:

上代码:

<!-- 标签页 -->
<view class="tabs">  
    <view class="tabs-item {{currentTab==0?'active':''}}" data-current="0" bindtap="clickTab">收入+9300</view>
    <view class="tabs-item {{currentTab==1?'active':''}}" data-current="1" bindtap="clickTab">支出-0</view>
</view>

<view  class="tab-content">
    <!-- 收入 -->
    <scroll-view scroll-y="true" bindscrolltolower="loadIncome" class="tab-list-content {{currentTab==0?'active':''}}">
        <view class="tab-list-item" wx:for="{{inList}}">
            <view class="tab-list-item-detail">
                <text>{{item.style}}</text>
                <text class="tab-list-item-after">{{item.coin}}</text>
            </view>
            <view class="tab-list-item-time">{{item.time}}</view>
        </view>
        <view hidden="{{isHidenLoadMore}}">
            <loading type="circle"></loading>
        </view>
    </scroll-view>
    <!-- 支出 -->
    <scroll-view scroll-y="true" bindscrolltolower="loadOutlay" class="tab-list-content {{currentTab==1?'active':''}}">
        <view class="tab-list-item" wx:for="{{outList}}">
            <view class="tab-list-item-detail">
                <text>{{item.style}}</text>
                <text class="tab-list-item-after">{{item.coin}}</text>
            </view>
            <view class="tab-list-item-time">{{item.time}}</view>
        </view>
        <view hidden="{{isHidenLoadMore}}">
            <loading type="circle"></loading>
        </view>
    </scroll-view>

</view>
.tabs{
    display: flex;
    margin: 0 24rpx;
    background-color: #fff;
    position: fixed;
    top: 300rpx;
    left: 0;
    right: 0;
}
.tabs-item{
    width: 50%;
    text-align: center;
    height: 66rpx;
    line-height: 66rpx;
    font-weight: bold;
}
.tabs-item.active{
    border-bottom: 2px solid rgb(58, 186, 248);
    color: rgb(58, 186, 248);
}
.tab-content{
    background-color: #fff;
    margin: 0 24rpx;
    /* margin-top:368rpx; */
    position: fixed;
    top: 368rpx;
    left: 0;
    right: 0;
    bottom: 0;
}
.tab-list-content{
    display: none;
    height: 100%;
}
.tab-list-content.active{
    display: block;
}
.tab-list-item{
    padding: 16rpx 24rpx;
    border-bottom: 1px solid rgb(229, 246, 253);
}
.tab-list-item-detail{
    display: flex;
    justify-content: space-between;
}
.tab-list-item-after{
    font-weight: bold;
}
.tab-list-item-time{
    color: rgb(136, 136, 136);
    font-size: 12px;
}
// pages/me-customer/me-customer.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isHidenLoadMore:true,
        currentTab:0,

        inList: [{ style: '签到', coin: '+100', time: '01:49:46' }, { style: '分享美文', coin: '+500', time: '05:49:46' }, { style: '签到', coin: '+100', time: '01:49:46' }, { style: '分享美文', coin: '+500', time: '05:49:46' }, { style: '签到', coin: '+100', time: '01:49:46' }, { style: '分享美文', coin: '+500', time: '05:49:46' }, { style: '签到', coin: '+100', time: '01:49:46' }, { style: '分享美文', coin: '+500', time: '05:49:46' }, { style: '签到', coin: '+100', time: '01:49:46' }, { style: '分享美文', coin: '+500', time: '05:49:46' }, { style: '签到', coin: '+100', time: '01:49:46' }, { style: '分享美文', coin: '+500', time: '05:49:46' }],

        outList: [{ style: '兑换礼品', coin: '-1000', time: '01:49:46' }, { style: '兑换礼品', coin: '-50000', time: '05:49:46' }, { style: '兑换礼品', coin: '-1000', time: '01:49:46' }, { style: '兑换礼品', coin: '-50000', time: '05:49:46' }, { style: '兑换礼品', coin: '-1000', time: '01:49:46' }, { style: '兑换礼品', coin: '-50000', time: '05:49:46' }, { style: '兑换礼品', coin: '-1000', time: '01:49:46' }, { style: '兑换礼品', coin: '-50000', time: '05:49:46' }, { style: '兑换礼品', coin: '-1000', time: '01:49:46' }, { style: '兑换礼品', coin: '-50000', time: '05:49:46' }, { style: '兑换礼品', coin: '-1000', time: '01:49:46' }, { style: '兑换礼品', coin: '-50000', time: '05:49:46' }, { style: '兑换礼品', coin: '-1000', time: '01:49:46' }, { style: '兑换礼品', coin: '-50000', time: '05:49:46' }]
    },

    clickTab:function(e){
        var current = e.currentTarget.dataset.current;
        this.setData({
            currentTab: current
        })
    },

    loadIncome:function(e){
        console.log("收入");
        console.log(e);

        var that = this;
        var maxNum = 30; //最多可加载条目

        var newList = [{ style: '签到', coin: '+50', time: '01:49:46' }, { style: '分享美文', coin: '+300', time: '05:49:46' }];

        var inList = that.data.inList;
        if (inList.length < maxNum) {
            that.setData({
                isHidenLoadMore: false//显示“加载符”
            });
            for (let val of newList) {
                inList.push(val);
            };

            setTimeout(function () {//模拟请求延迟个过程,实际可以不用要setTimeout
                that.setData({
                    isHidenLoadMore: true,
                    inList: inList
                })
            }, 2000);
        }
    },

    loadOutlay: function (e) {
        console.log("支出");
        console.log(e);

        var that = this;
        var maxNum = 30; //最多可加载条目

        var newList = [{ style: '签到', coin: '-150', time: '01:49:46' }, { style: '分享美文', coin: '-600', time: '05:49:46' }];

        var outList = that.data.outList;
        if (outList.length < maxNum) {
            that.setData({
                isHidenLoadMore: false//显示“加载符”
            });
            for (let val of newList) {
                outList.push(val);
            };

            setTimeout(function () {//模拟请求延迟个过程,实际可以不用要setTimeout
                that.setData({
                    isHidenLoadMore: true,
                    outList: outList
                })
            }, 2000);
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        // var that = this;
        // var maxNum = 30; //最多可加载条目

        // var newList = [{ style: '签到', coin: '+50', time: '01:49:46' }, { style: '分享美文', coin: '+300', time: '05:49:46' }];

        // var inList = that.data.inList;
        // if (inList.length < maxNum) {
        //     that.setData({
        //         isHidenLoadMore: false//显示“加载符”
        //     });
        //     for (let val of newList) {
        //         inList.push(val);
        //     };

        //     setTimeout(function () {//模拟请求延迟个过程,实际可以不用要setTimeout
        //         that.setData({
        //             isHidenLoadMore: true,
        //             inList: inList
        //         })
        //     }, 2000);
        // }
    },


})

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
使用 Bootstrap 实现 Tab 标签切换显示非常简单,只需要使用 Bootstrap 的 Tab 插件和一些基本的 HTML 和 CSS 代码即可。以下是一个基本的示例: HTML 代码: ``` <ul class="nav nav-tabs"> <li class="active"><a href="#tab1" data-toggle="tab">Tab 1</a></li> <li><a href="#tab2" data-toggle="tab">Tab 2</a></li> <li><a href="#tab3" data-toggle="tab">Tab 3</a></li> </ul> <div class="tab-content"> <div class="tab-pane active" id="tab1"> <p>这是 Tab 1 中的内容。</p> </div> <div class="tab-pane" id="tab2"> <p>这是 Tab 2 中的内容。</p> </div> <div class="tab-pane" id="tab3"> <p>这是 Tab 3 中的内容。</p> </div> </div> ``` 在这个示例中,我们使用了 Bootstrap 的 nav 和 tab 样式来创建了一个 Tab 标签每个 Tab 都是一个链接,点击链接可以切换到对应的标签。我们使用了 data-toggle 属性来告诉 Bootstrap 使用 Tab 插件来处理这些链接。每个标签都是一个 div 元素,使用了 tab-pane 样式。其中,第一个标签使用了 active 样式来标记为默认显示的标签。 CSS 代码: ``` .nav-tabs { margin-bottom: 15px; } .tab-content { padding: 15px; border: 1px solid #ddd; border-top: none; } ``` 在这个示例中,我们使用了一些基本的 CSS 样式来调整 Tab 标签的外观。我们设置了一个 margin-bottom 样式来增加 Tab 标签和其他元素之间的间距,以及一个 padding 和 border 样式来设置标签的边框和内边距。 JavaScript 代码: ``` $(function() { $('.nav-tabs a').click(function() { $(this).tab('show'); }); }); ``` 在这个示例中,我们使用了 jQuery 库来管理 Tab 插件。当用户点击 Tab 标签中的链接时,我们使用 tab('show') 方法来显示对应的标签。 总之,使用 Bootstrap 实现 Tab 标签切换显示非常简单,只需要使用一些基本的 HTML、CSS 和 JavaScript 代码即可。通过使用 Tab 插件,我们可以轻松创建漂亮且易于使用的 Tab 标签,为用户提供更好的体验。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值