vue2.0 移动端,下拉刷新,上拉加载更多 封装组件
前言
在做移动端的避免不了 下拉刷新,上拉加载
直接上代码吧,哈哈
组件里:
<template lang="html">
<div class="yo-scroll"
:class="{'down':(state===0),'up':(state==1),refresh:(state===2),touch:touching}"
@touchstart="touchStart($event)"
@touchmove="touchMove($event)"
@touchend="touchEnd($event)"
@scroll="(onInfinite || infiniteLoading) ? onScroll($event) : undefined">
<section class="inner" :style="{ transform: 'translate3d(0, ' + top + 'px, 0)' }">
<header class="pull-refresh">
<slot name="pull-refresh">
<span class="down-tip">下拉更新</span>
<span class="up-tip">松开更新</span>
<span class="refresh-tip">更新中……</span>
</slot>
</header>
<slot></slot>
<footer class="load-more">
<slot name="load-more">
<span>{{loadingText}}</span>
</slot>
</footer>
</section>
</div>
</template> <script>
export default {
name: 'kl-scroll',
props: {
offset: {
type: Number,
default: 40
},
loadingText: {
type: String,
default: '加载中...'
},
enableInfinite: {
type: Boolean,
default: true
},
enableRefresh: {
type: Boolean,
default: true
},
onRefresh: {
type: Function,
default: undefined,
required: false
},
onInfinite: {
type: Function,
default: undefined,
require: false
}
},
data() {
return {
top: 0,
state: 0,
startY: 0,
touching: false,
infiniteLoading: false
}
},
methods: {
touchStart(e) {
this.startY = e.targetTouches[0].pageY
this.startScroll = this.$el.scrollTop || 0
this.touching = true
},
touchMove(e) {
if (!this.enableRefresh || this.$el.scrollTop > 0 || !this.touching) {
return
}
let diff = e.targetTouches[0].pageY - this.startY - this.startScroll
if (diff > 0) e.preventDefault()
this.top = Math.pow(diff, 0.8) + (this.state === 2 ? this.offset : 0) if (this.state === 2) { // in refreshing
return
}
if (this.top >= this.offset) {
this.state = 1
} else {
this.state = 0
}
},
touchEnd(e) {
if (!this.enableRefresh) return
this.touching = false
if (this.state === 2) { // in refreshing
this.state = 2
this.top = this.offset
return
}
if (this.top >= this.offset) { // do refresh
this.refresh()
} else { // cancel refresh
this.state = 0
this.top = 0
}
},
refresh() {
this.state = 2
this.top = this.offset
this.onRefresh(this.refreshDone)
},
refreshDone() {
this.state = 0
this.top = 0
this.infiniteLoading = false
}, infinite() {
this.infiniteLoading = true
this.onInfinite(this.infiniteDone)
}, infiniteDone() {
this.infiniteLoading = false
}, onScroll(e) {
if (!this.enableInfinite || this.infiniteLoading) {
return
}
let outerHeight = this.$el.clientHeight
let innerHeight = this.$el.querySelector('.inner').clientHeight
let scrollTop = this.$el.scrollTop
let ptrHeight = this.onRefresh ? this.$el.querySelector('.pull-refresh').clientHeight : 0
let infiniteHeight = this.$el.querySelector('.load-more').clientHeight
let bottom = innerHeight - outerHeight - scrollTop - ptrHeight
if (bottom < infiniteHeight) this.infinite()
}
}
}
</script>
<style >
.yo-scroll {
position: absolute;
/* top: 2.5rem; */
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow: auto;
-webkit-overflow-scrolling: touch;
background-color: #f5f8fa;
}
.yo-scroll .inner {
position: absolute;
top: -2rem;
width: 100%;
transition-duration: 300ms;
}
.yo-scroll .pull-refresh {
position: relative;
left: 0;
top: 0;
width: 100%;
height: 2rem;
display: flex;
align-items: center;
justify-content: center;
}
.yo-scroll.touch .inner {
transition-duration: 0ms;
}
.yo-scroll.down .down-tip {
display: block;
}
.yo-scroll.up .up-tip {
display: block;
}
.yo-scroll.refresh .refresh-tip {
display: block;
}
.yo-scroll .down-tip,
.yo-scroll .refresh-tip,
.yo-scroll .up-tip {
display: none;
}
.yo-scroll .load-more {
height: 1rem;
display: flex;
align-items: center;
justify-content: center;
}
</style>
然后
把上面组件拷贝一下,保存vue文件refresh.vue放到你的component/common文件夹下, 然后引入到页面 ,
下面是引用我的demo
<template>
<kl-scroll :on-refresh="onRefresh" :on-infinite="onInfinite" :loading-text="loadingText">
<section class="app-body container">
<div class="container top30r record-table">
<div class="record-header">
<span class="label-time">提款时间</span>
<span class="label-withdraw-amount">提现金额</span>
<span class="label-arrival-amount">到帐金额</span>
<span class="label-status">到账状态</span>
</div>
<div class="record-body">
<div class="record-body-row" v-for="(item,index) in records" :key="index">
<span class="time">{{item._created_at}}</span>
<span class="withdraw-amount">{{item.withdraw_amount}}</span>
<span class="arrival-amount">{{item.arrival_amount}}</span>
<span
class="status"
:class="item.status == withdrawStatus.arrival ? 'label-green' : ''"
>{{item._status}}</span>
</div>
</div>
</div>
</section>
</kl-scroll>
</template> <script>
import api from "@/api";
import qs from "qs";
export default {
name: "withdrawRecord",
data() {
return {
withdrawStatus: {
arrival: 1
},
records: [
// {
// _created_at: "2019-03-07 15:20",
// withdraw_amount: "137854",
// arrival_amount: "13552.55",
// status: 1
// }
],
loadingText: "加载中...",
page: 0, //当前页面
num: 20, // 一次显示多少条
listdata: [], // 下拉更新数据存放数组
downdata: [] // 上拉更多的数据存放数组
};
},
beforeCreate() {
this.$loading.open();
},
mounted() {
let vm = this;
this.$nextTick(() => {
vm.getList();
});
},
methods: {
onRefresh(done) {
let _this = this;
_this.counter = 1;
_this.$el.querySelector(".load-more").style.display = "flex";
_this.loadingText = "加载中……";
_this.getList();
done(); // call done
},
onInfinite(done) {
let _this = this;
let arr = [];
for (var i = 0; i < 10; i++) {
arr.push({
_created_at: "2019-03-07 15:20",
withdraw_amount: 155 + i,
arrival_amount: "13552.55",
status: 1,
_status: "到賬"
});
}
setTimeout(() => {
_this.records = _this.records.concat(arr);
}, 300); if (arr.length < _this.num) {
_this.loadingText = "加载完毕……";
//vm.$el.querySelector('.load-more').style.display = 'none';
return;
} else {
_this.counter++;
}
done(); // call done
},
getList() {
let _this = this;
// let arr = [];
// for (var i = 0; i < 40; i++) {
// arr.push({
// _created_at: "2019-03-07 15:20",
// withdraw_amount: 155 + i,
// arrival_amount: "13552.55",
// status: 1,
// _status: "到賬"
// });
// } // _this.records = arr;
// if (arr.length >= _this.num) {
// _this.counter++;
// }
let urlParams = {
urlParams: {
page: _this.page
}
};
api.withdrawRecords(urlParams).then(res => {
if (res.data) {
let data = res.data.data;
_this.records = data.records;
if (_this.records.length == 0 || _this.records.length < _this.num) {
_this.loadingText = "加载完毕……";
_this.$el.querySelector(".load-more").style.display = "none";
}
_this.$loading.close();
}
});
}
}
};
</script> <style lang="scss" scoped>
好啦 看下效果图
vue2.0 移动端,下拉刷新,上拉加载更多 封装组件的更多相关文章
- SwipeRefreshLayout实现下拉刷新上滑加载
1. 效果图 2.RefreshLayout.java package myapplication.com.myapplication; import android.content.Context; ...
- Android 下拉刷新上啦加载SmartRefreshLayout + RecyclerView
在弄android刷新的时候,可算是耗费了一番功夫,最后发觉有现成的控件,并且非常好用,这里记录一下. 原文是 https://blog.csdn.net/huangxin112/article/de ...
- juery下拉刷新,div加载更多元素并添加点击事件(二)
buffer.append("<div class='col-xs-3 "+companyId+"' style='padding-left: 10px; padd ...
- 移动端下拉刷新上拉加载-mescroll.js插件
最近无意间看到有这么一个上拉刷新下拉加载的插件 -- mescroll.js,个人感觉挺好用的,官网地址是:http://www.mescroll.com 然后我就看了一下文档,简单的写了一个小dem ...
- Android如何定制一个下拉刷新,上滑加载更多的容器
前言 下拉刷新和上滑加载更多,是一种比较常用的列表数据交互方式. android提供了原生的下拉刷新容器 SwipeRefreshLayout,可惜样式不能定制. 于是打算自己实现一个专用的.但是下拉 ...
- Android 自定义 ListView 上下拉动“刷新最新”和“加载更多”歌曲列表
本文内容 环境 测试数据 项目结构 演示 参考资料 本文演示,上拉刷新最新的歌曲列表,和下拉加载更多的歌曲列表.所谓"刷新最新"和"加载更多"是指日期.演示代码 ...
- 基于SwiperJs的H5/移动端下拉刷新上拉加载更多的效果
最早时,公司的H5项目中曾用过点击一个"加载更多"的DOM元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...
- 基于SwiperJs的H5/移动端下拉刷新上拉加载更多
最早时,公司的H5项目中曾用过点击一个"加载更多"的DOM元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...
- vue10行代码实现上拉翻页加载更多数据,纯手写js实现下拉刷新上拉翻页不引用任何第三方插件
vue10行代码实现上拉翻页加载更多数据,纯手写js实现下拉刷新上拉翻页不引用任何第三方插件/库 一提到移动端的下拉刷新上拉翻页,你可能就会想到iScroll插件,没错iScroll是一个高性能,资源 ...
随机推荐
- 【HANA系列】SAP HANA SQL计算两个日期的差值
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL计算两个 ...
- 【HANA系列】SAP HANA SQL合并多行操作
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL合并多行 ...
- Java架构师 -- 知识库
1,CSDN知识库: http://lib.csdn.net/base/architecture 2,淘宝
- DES、AES和RSA加密算法
DES加密算法简介 DES(Data Encryption Standard)是目前最为流行的加密算法之一(它是分组密码). 强加密使用的基本操作 -> 混淆与扩散 混淆:是一种使密钥与密文之间 ...
- PTA(Basic Level)1057.数零壹
给定一串长度不超过 105 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一下 N 的二进制表示中有多少 0.多少 1.例如 ...
- 使用正则实现php的trim函数,支持全角空格
之前使用trim来移除一段文字开头的空格,移除不掉,发现是全角空格的锅. 便专门添加对全角空格的移除: trim($str," "); 但是效果并不好,因为trim函数对多字节字符 ...
- 你写的 Java 代码是如何一步步输出结果的? (转)
出处: 一步步解析java执行内幕 对于任何一门语言,要想达到精通的水平,研究它的执行原理(或者叫底层机制)不失为一种良好的方式.在本篇文章中,将重点研究java源代码的执行原理,即从程 序员编写J ...
- 牛客 4C Alliances (dfs序)
大意: 给定树, 有$k$个帮派, 第$i$个帮派所占据点为$c_i$, 以及$c_i$两两相连路径上的所有点. 一个点可能被多个帮派占领. $q$个询问, 第$i$个询问给定$t_i$个帮派, 给定 ...
- 福建工程学院第十四届ACM校赛B题题解
第二集,未来的我发量这么捉急的吗 题意: 有n个数,请问有多少对数字(i,j)(1<=i<j<=n),满足(a[i]^a[j])+((a[i]&a[j])<<1) ...
- python的Email提醒
目的意义 使用Email自动发送,有利于实时获取爬取信息,更方便的掌握要闻. 导入相关库 MINEText库定义了发送信息, Header定义了发送的主题 formate定义了收件人和发件人的格式信息 ...