微信小程序实战之上拉分页加载效果2

上拉加载(分页加载)

当用户打开一个页面时,假设后台数据量庞大时,一次性地返回所有数据给客户端,页面的打开速度就会有所下降,而且用户只看上面的内容而不需要看后面的内容时,也浪费用户流量,基于优化的角度来考虑,后台不要一次性返回所有数据,当用户有需要再往下翻的时候,再加载更加数据出来。

业务需求:

列表滚动到底部时,继续往上拉,加载更多内容

必备参数:

(1)pageindex: 1 //第几次加载 (2)callbackcount: 15 //需要返回数据的个数

其他参数:

根据接口的所需参数

实现原理:

当第一次访问接口时,传递2个必备参数(第1次加载,需要返回数据的个数为15个),和其他参数(需要搜索的字符串)给后台,后台返回第一次数据过来。在请求成功的的回调函数中,判断返回的数据是否>0,是,则取出数据,渲染视图层,并把“上拉加载”显示在列表底部;否,则没有数据可取,并把“没有更多”显示在列表底部,同时把“上拉加载”隐藏掉。

当用户已经滚动到列表底部(这里使用到小程序提供的scroll-view组件的bindscrolltolower事件),触发bindscrolltolower事件,参数pageindex+1,再把2个必备参数(第2次加载,需要返回数据的个数为15个)和其他参数(需要搜索的字符串)给后台,后台把其余的数据返回给前台,前台在原来数据的基础上添加数据。

示例: wxml:

js:

util.js:

module.exports = {
getSearchMusic: getSearchMusic
}

wxss:

/搜索/
.search{
flex: auto;
display: flex;
flex-direction: column;
background: #fff;
}
.search-bar{
flex: none;
display: flex;
align-items: center;
justify-content: space-between;
padding: 20rpx;
background: #f4f4f4;
}
.search-wrap{
position: relative;
flex: auto;
display: flex;
align-items: center;
height: 80rpx;
padding: 0 20rpx;
background: #fff;
border-radius: 6rpx;
}
.search-wrap .icon-search{
margin-right: 10rpx;
}
.search-wrap .search-input{
flex: auto;
font-size: 28rpx;
}
.search-cancel{
padding: 0 20rpx;
font-size: 28rpx;
}

/搜索结果/
.search-result{
flex: auto;
position: relative;
}
.search-result scroll-view{
position: absolute;
bottom: 0;
left: 0;
right: 0;
top: 0;
}
.result-item{
position: relative;
display: flex;
flex-direction: column;
padding: 20rpx 0 20rpx 110rpx;
overflow: hidden;
border-bottom: 2rpx solid #e5e5e5;
}

.result-item .media{
position: absolute;
left: 16rpx;
top: 16rpx;
width: 80rpx;
height: 80rpx;
border-radius: 999rpx;
}
.result-item .title,.result-item .subtitle{
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
line-height: 36rpx;
}
.result-item .title{
margin-bottom: 4rpx;
color: #000;
}
.result-item .subtitle{
color: #808080;
font-size: 24rpx;
}
.result-item:first-child .subtitle text{
margin-right: 20rpx;
}
.result-item:not(:first-child) .subtitle text:not(:first-child):before{
content: '/';
margin: 0 8rpx;
}
.loading{
padding: 10rpx;
text-align: center;
}
.loading:before{
display: inline-block;
margin-right: 5rpx;
vertical-align: middle;
content: '';
width: 40rpx;
height: 40rpx;
background: url(../../images/icon-loading.png) no-repeat;
background-size: contain;
animation: rotate 1s linear infinite;
}
.loading.complete:before{
display: none;
}

运行:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

概述 消息能力是小程序能力中的重要组成,我们为开发者提供了...
判断H5页面环境在微信中还是小程序中 用小程序提供的wx.mini...
wx.reLaunch和wx.navigateTo,wx.navigateTo的区别 2019-03-...
微信小程序如何从数组里取值_微信小程序 传值取值的几种方法...
H5项目接入微信授权登录,通过 UA 区分微信还是普通浏览器:...
微信小程序获取data-xx=""属性的值,自定...