前言

在做移动端的避免不了 下拉刷新,上拉加载

直接上代码吧,哈哈

组件里:

 <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 移动端,下拉刷新,上拉加载更多 封装组件的更多相关文章

  1. SwipeRefreshLayout实现下拉刷新上滑加载

    1. 效果图 2.RefreshLayout.java package myapplication.com.myapplication; import android.content.Context; ...

  2. Android 下拉刷新上啦加载SmartRefreshLayout + RecyclerView

    在弄android刷新的时候,可算是耗费了一番功夫,最后发觉有现成的控件,并且非常好用,这里记录一下. 原文是 https://blog.csdn.net/huangxin112/article/de ...

  3. juery下拉刷新,div加载更多元素并添加点击事件(二)

    buffer.append("<div class='col-xs-3 "+companyId+"' style='padding-left: 10px; padd ...

  4. 移动端下拉刷新上拉加载-mescroll.js插件

    最近无意间看到有这么一个上拉刷新下拉加载的插件 -- mescroll.js,个人感觉挺好用的,官网地址是:http://www.mescroll.com 然后我就看了一下文档,简单的写了一个小dem ...

  5. Android如何定制一个下拉刷新,上滑加载更多的容器

    前言 下拉刷新和上滑加载更多,是一种比较常用的列表数据交互方式. android提供了原生的下拉刷新容器 SwipeRefreshLayout,可惜样式不能定制. 于是打算自己实现一个专用的.但是下拉 ...

  6. Android 自定义 ListView 上下拉动“刷新最新”和“加载更多”歌曲列表

    本文内容 环境 测试数据 项目结构 演示 参考资料 本文演示,上拉刷新最新的歌曲列表,和下拉加载更多的歌曲列表.所谓"刷新最新"和"加载更多"是指日期.演示代码 ...

  7. 基于SwiperJs的H5/移动端下拉刷新上拉加载更多的效果

    最早时,公司的H5项目中曾用过点击一个"加载更多"的DOM元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...

  8. 基于SwiperJs的H5/移动端下拉刷新上拉加载更多

    最早时,公司的H5项目中曾用过点击一个"加载更多"的DOM元素来实现分页的功能,后来又用过网上有人写的一个上拉加载更多的插件,那个插件是页面将要滚动到底部时就自动请求数据并插入到页 ...

  9. vue10行代码实现上拉翻页加载更多数据,纯手写js实现下拉刷新上拉翻页不引用任何第三方插件

    vue10行代码实现上拉翻页加载更多数据,纯手写js实现下拉刷新上拉翻页不引用任何第三方插件/库 一提到移动端的下拉刷新上拉翻页,你可能就会想到iScroll插件,没错iScroll是一个高性能,资源 ...

随机推荐

  1. 【HANA系列】SAP HANA SLT 在表中隐藏字段并传入HANA的方法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SLT在表中隐 ...

  2. NOIp2018D1T2 货币系统【分析&完全背包】

    题目传送门 看到题目瞬间想起某凯的疑惑,感觉不会做....然后观察样例可以知道,去掉原来货币系统中能够被其他币值凑出来的数就是答案(样例分析法),然后就完事了(huaji). 简单理解一下吧: 首先, ...

  3. 【VS开发】关于SEH的简单总结

    尽管以前写过一篇SEH相关的文章<关于SEH的简单总结>, 但那真的只是皮毛,一直对Windows异常处理的原理似懂非懂, 看了下面的文章 ,一切都豁然开朗.  1997年文章,Windo ...

  4. Linux基础命令---间歇执行命令---watch

    [watch] watch指令可以间歇性的执行程序,将输出结果以全屏的方式显示,默认是2s执行一次. watch指令下发后,将会一直被执行,直到被中断. [语法] watch \ [-d h v t] ...

  5. LeetCode-求最长回文子序列

    题目:给定一个字符串,求它的最长回文子串 /*求最长回文子串,以当前字符为中心,向两边同时拓展*/ string longestPalindrome(string s) { int len = s.l ...

  6. # 匈牙利算法(二分图最大匹配)- hdu 过山车

    匈牙利算法(二分图最大匹配)- hdu 过山车 Hdu 2063 二分图:图中的点可以分成两组U,V,所有边都是连接U,V中的顶点.等价定义是:含奇数条边的图. 匹配:一个匹配是一个边的集合,其中任意 ...

  7. 微信小程序,预览在开发工具上显示正常,手机预览二维码报request->fail错误,打开手机的调试功能又正常。

    这里错误很明显是属于网址错误,开发工具和手机调试都能走request->success: 唯独常规模式下无法显示. 最开始调试过很多方法,没找出原因.最后到小程序开发设置才发现,自己未配置服务器 ...

  8. 继续:Ruby on Rails 简单了解

    一. 接着上一篇继续 1.限制微博的长度 在 Rails 中实现这种限制很简单,使用验证(validation)功能即可.要限制微博的长度最多为 140 个字符 (1).打开文件:app/models ...

  9. WPF ListView多行显示

    //前台 <ListView Margin="14,152,12,74" Name="lvList" SelectionMode="Multip ...

  10. @RequestMapping-@PathVariable小误区

    去掉勾选就可以演示出错误了,一般勾选是为了方便我们Debug调试 会出现500错误: 正确的写法: