uni-app实现上拉加载,下拉刷新(下拉带动画)

直接代码展示了

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>

具体说明代码里面已经注释好了

现在将代码复制过去就能调用了,赶快试试吧

 

  • 16
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随风小薇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值