订阅本栏目 RSS您所在的位置: 深山工作室 > uni-app > 正文

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

2020/9/15 15:20:52 字体: 浏览 7119

直接展示代码,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

后一页:没有了
相关阅读
深山旅行社管理系统商业版增加线路日期报价功能样式选择(增加了3个日期报价效果)
旅行社线路主题默认模板5
kiss草原旅游网站
ASP操作access或sqlserver数据库的函数库
Javascript实现CSS代码高亮显示
利用锚点做纯CSS的图片展示效果
秀色摄影
JS获取网页中HTML元素的几种方法
共有0条关于《uni-app实现上拉加载,下拉刷新(下拉带动画)》的评论
发表评论
正在加载评论......
返回顶部发表评论
呢 称:
表 情:
内 容:
评论内容:不能超过 1000 字,需审核,请自觉遵守互联网相关政策法规。
验证码: 验证码 
网友评论声明,请自觉遵守互联网相关政策法规。

您发布的评论即表示同意遵守以下条款:
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家、社会、集体和公民的合法权益;
二、不得发布国家法律、法规明令禁止的内容;互相尊重,对自己在本站的言论和行为负责;
三、本站对您所发布内容拥有处置权。

更多信息>>栏目类别选择
百度小程序开发
微信小程序开发
微信公众号开发
uni-app
asp函数库
ASP
DIV+CSS
HTML
python
更多>>同类信息
uni-app开发表单input组件的一些规则说明自己预留使用
uni-app:使用uni.downloadFile下载word或pdf文件并保存到手机
小程序中利用addPhoneContact将联系人的信息添加到手机通讯录支持保存联系人头像
微信小程序打开客服提示:该小程序提供的服务出现故障,请稍后重试
微信小程序客服会话只能过button让用户主动触发
uni-app开发微信小程序使用button的open-type为contact调用微信客服不能用view或者js调用
更多>>最新添加文章
在Android、iOS、Windows、MacOS中微信小程序的文件存放路径
python通过代码修改pip下载源让下载库飞起
python里面requests.post返回的res.text还有其它的吗
aliyun阿里云续费域名优惠口令(注册、续费都可以使用)
windows7环境下安装配置jdk
python对微信操作要用到这两个库wxpy与itchat
ASP中Utf-8与Gb2312编码转换乱码问题的解决方法页面编码声明
DW设置之后更好用 DreamweaverCS编辑GB2312与UTF-8文件在代码视图中点击鼠标错位问题的解决办法
更多>>随机抽取信息
CSS编写过程中常见的10个错误以及解决方法
旅行社手机网站模板8
asp中最难发现的错误由最简单的程序引起
19寸宽屏旅行社网站模板8
支持火狐,IE6.ie7.ie8.ie9的加入收藏/设为首页代码
asp利用XMLHTTP加载动态页面并且生成静态页面