![](/images/pub/focusimg.jpg)
直接展示代码,uni-app的上拉加载动画
1 . 在pages.json添加允许下拉刷新
{
"path":"pages/lookuser/lookuser",
"style":{
"navigationBarTitleText":"用户日志",
"enablePullDownRefresh": true//就是这个
}
}
2. 上拉加载更多组件
比如把组件放在component下了component/uni-load-more.vue
<template>
<view class="load-more">
<view class="loading-img" v-show="loadingType === 1 && showImage">
<view class="load1">
<view :style="{background:color}"></view>
<view :style="{background:color}"></view>
<view :style="{background:color}"></view>
<view :style="{background:color}"></view>
</view>
<view class="load2">
<view :style="{background:color}"></view>
<view :style="{background:color}"></view>
<view :style="{background:color}"></view>
<view :style="{background:color}"></view>
</view>
<view class="load3">
<view :style="{background:color}"></view>
<view :style="{background:color}"></view>
<view :style="{background:color}"></view>
<view :style="{background:color}"></view>
</view>
</view>
<text class="loading-text" :style="{color:color}">
{{loadingType === 0 ? contentText.contentdown : (loadingType === 1 ? contentText.contentrefresh : contentText.contentnomore)}}</text>
</view>
</template>
<script>
export default {
name: "load-more",
props: {
loadingType: {
//上拉的状态:0-loading前;1-loading中;2-没有更多了
type: Number,
default: 0
},
showImage: {
type: Boolean,
default: true
},
color: {
type: String,
default: "#777777"
},
contentText: {
type: Object,
default () {
return {
contentdown: "上拉显示更多",
contentrefresh: "正在加载...",
contentnomore: "没有更多数据了"
};
}
}
},
data() {
return {}
}
}
</script>
<style>
.load-more{display:flex;flex-direction:row;height:80upx;align-items:center;justify-content:center;}
.loading-img{height:24px;width:24px;margin-right:10px;}
.loading-text{font-size:28upx;color:#777777;}
.loading-img>view{position:absolute;}
.load1,.load2,.load3{height:24px;width:24px;}
.load2{transform:rotate(30deg);}
.load3{transform:rotate(60deg);}
.loading-img>view view{width:6px;height:2px;border-top-left-radius:1px;border-bottom-left-radius:1px;background:#777;position:absolute;opacity:0.2;transform-origin:50%;-webkit-animation:load 1.56s ease infinite;}
.loading-img>view view:nth-child(1){transform:rotate(90deg);top:2px;left:9px;}
.loading-img>view view:nth-child(2){-webkit-transform:rotate(180deg);top:11px;right:0px;}
.loading-img>view view:nth-child(3){transform:rotate(270deg);bottom:2px;left:9px;}
.loading-img>view view:nth-child(4){top:11px;left:0px;}
.load1 view:nth-child(1){animation-delay:0s;}
.load2 view:nth-child(1){animation-delay:0.13s;}
.load3 view:nth-child(1){animation-delay:0.26s;}
.load1 view:nth-child(2){animation-delay:0.39s;}
.load2 view:nth-child(2){animation-delay:0.52s;}
.load3 view:nth-child(2){animation-delay:0.65s;}
.load1 view:nth-child(3){animation-delay:0.78s;}
.load2 view:nth-child(3){animation-delay:0.91s;}
.load3 view:nth-child(3){animation-delay:1.04s;}
.load1 view:nth-child(4){animation-delay:1.17s;}
.load2 view:nth-child(4){animation-delay:1.30s;}
.load3 view:nth-child(4){animation-delay:1.43s;}
@-webkit-keyframes load{0%{opacity:1;}
100%{opacity:0.2;}
}
</style>
3. 在页面上调用组件
<template>
<view style="flex: 1;">
<view v-for="(item, index) in newsList" class="newslist">{{item}}</view>
<!--3使用组件 -->
<uni-load-more :loadingType="loadingType" :contentText="contentText" ></uni-load-more>
</view>
</template>
<script>
//1引入组件 uni-load-more.vue
import uniLoadMore from '../../component/uni-load-more.vue';
var _self,
page = 1,
timer = null;
// 定义全局参数,控制数据加载
export default {
components: {//2注册组件
uniLoadMore
},
data: {
newsList: [],
loadingText: '加载中...',
loadingType: 0,//定义加载方式 0---contentdown 1---contentrefresh 2---contentnomore
contentText: {
contentdown:'上拉显示更多',
contentrefresh: '正在加载...',
contentnomore: '没有更多数据了'
}
},
onLoad: function() {
_self = this;
//页面一加载时请求一次数据
this.getnewsList();
},
onPullDownRefresh: function() {
//下拉刷新的时候请求一次数据
this.getnewsList();
},
onReachBottom: function() {
//触底的时候请求数据,即为上拉加载更多
//为了更加清楚的看到效果,添加了定时器
if (timer != null) {
clearTimeout(timer);
}
timer = setTimeout(function() {
_self.getmorenews();
}, 1000);
// 正常应为:
// _self.getmorenews();
},
methods: {
getmorenews: function() {
if (_self.loadingType !== 0) {//loadingType!=0;直接返回
return false;
}
_self.loadingType = 1;
uni.showNavigationBarLoading();//显示加载动画
uni.request({
url:'https://demo.hcoder.net/index.php?user=hcoder&pwd=hcoder&m=list1&page=' + page,
method: 'GET',
success: function(res) {
console.log(JSON.stringify(res));
if (res.data == null) {//没有数据
_self.loadingType = 2;
uni.hideNavigationBarLoading();//关闭加载动画
return;
}
page++;//每触底一次 page +1
_self.newsList = _self.newsList.concat(res.data.split('--hcSplitor--'));//将数据拼接在一起
_self.loadingType = 0;//将loadingType归0重置
uni.hideNavigationBarLoading();//关闭加载动画
}
});
},
getnewsList: function() {//第一次回去数据
page = 1;
this.loadingType = 0;
uni.showNavigationBarLoading();
uni.request({
url: 'https://demo.hcoder.net/index.php?user=hcoder&pwd=hcoder&m=list1&page=1',
method: 'GET',
success: function(res) {
page++;//得到数据之后page+1
_self.newsList = res.data.split('--hcSplitor--');
uni.hideNavigationBarLoading();
uni.stopPullDownRefresh();//得到数据后停止下拉刷新
}
});
}
}
};
</script>
<style>
.newslist{padding:10px;line-height:60px;font-size:28px;}
.loading{text-align:center;line-height:80px;}
</style>
直接复制过去就能调用了,赶快试试吧
来源地址:https://blog.csdn.net/qq_39197547/article/details/84832913
您发布的评论即表示同意遵守以下条款:
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家、社会、集体和公民的合法权益;
二、不得发布国家法律、法规明令禁止的内容;互相尊重,对自己在本站的言论和行为负责;
三、本站对您所发布内容拥有处置权。