前言

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

直接上代码吧,哈哈

组件里:

 <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. 【PP系列】SAP PP模块工作中心主数据维护

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[PP系列]SAP PP模块工作中心主数据维护 ...

  2. Object的构造函数方法

    参考自MDN Object 构造函数的方法 1.Object.assign() 将可枚举属性的值从一个或多个源对象复制到目标对象. 2.Object.create() 创建一个新对象,继承现有对象的构 ...

  3. 海量数据和高并发下的 Redis 业务优化实践

    本文内容是我在 6 月 23 日参加的深圳 GIAC 技术大会上演讲的文字稿. 观众朋友们,我是来自掌阅的工程师钱文品,掘金小册<Redis 深度历险>的作者.今天我带来的是分享主题是:R ...

  4. SpringMVC基础教程

    1. 最简单的配置 首先是要有相应的配置文件: 文件内容: <context:component-scan base-package="com.imethsoft.server.*&q ...

  5. 同sql server不同database间的数据访问

    虽未经测试,但是应该是登陆名同时具有此2数据库访问权限啦. select * from [basename].dbo.[tablename] done.

  6. mybatis N+1问题解决

    关联嵌套查询 示例: <resultMap id="blogResult" type="Blog"> <association propert ...

  7. 计算机网络--TCP三次握手和四次挥手

    TCP(传输控制协议) TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议.通过三次握手建立连接,通讯完成时要拆除连 ...

  8. AWS In Action

    Core Services of AWS Elastic Cloud Compute(EC2) Simple Storage Service(S3) Relational Database Servi ...

  9. 在Powershell中使用Group-Object和-GroupBy

    使用Group-Object(group)按组统计 PS C:\> Get-Command -Module Microsoft.PowerShell.LocalAccounts | group ...

  10. 解决mac下brew install报错

    Error: Another active Homebrew update process is already in progress.Please wait for it to finish or ...