前言

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

直接上代码吧,哈哈

组件里:

 <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. 【C/C++】什么是线程安全

    <strong>线程安全</strong>就是多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读取完,其他线程才可使用.不 ...

  2. sqlalchemy链接数据库

    from sqlalchemy import create_engine HOSTNAME = '127.0.0.1' PORT = 3306 DATABASE = 'first_sqlalchemy ...

  3. 菜鸟系列Fabric——Fabric 1.4共识机制(5)

    fabric 共识机制 由于fabric是分布式的系统,因此需要共识机制来保障各个节点以相同的顺序状态保存账本,达成一致性. 在当前fabric1.4版本中,存在三种共识机制,分别是solo,kafk ...

  4. 【数据库】Redis/MongoDB/MySQL/Oracle随笔索引

    数据库体系 [思维导图]数据库体系 密码: a8ni Redis JPA

  5. mysql语法难点

    select * from emp where comm is null or comm=0;/*没有提成的员工*/ 查询有提成的员工所有信息 select * from emp where comm ...

  6. Selenium在IE浏览器中执行脚本时输入字符太慢问题解决方法

    问题描述: IE浏览器中执行Selenium脚本的时候发现输入文本的时候总是一个字符一个字符的输入,并且输入每个字符之间都隔几秒.   解决方案: 将使用的IE浏览器驱动由64位的换成32位的.

  7. 使用mvn archetype:generate快速建立Maven项目目录结构

    1.mvn archetype:generate  按照提示进行选择,默认选的话可以直接按回车键 2.mvn archetype:generate -DgroupId=组织名,公司网址的反写+项目名 ...

  8. [Luogu5686] 和积和

    Description 给定两个下标从\(1\)到\(n\)编号的序列 \(a_i,b_i\),定义函数\(S(l,r)(1\le l\le r\le n)\)为: \[\sum_{i=l}^r a_ ...

  9. 坦克大战--Java类型 ---- (3)实现socket通信

    一.实现思路 使用socket通信的一些方法来实现socket通信,客户端和服务端两边需要约定好通信的接口Port(尽量选高的),客户端需要服务端的IP地址,以实现数据交流. 同时,客户端和服务端需要 ...

  10. docopt 安装及基本应用

    什么是 docopt docopt是一种python 编写的命令行执行脚本的交互语言. 它是一种语言! 它是一种语言! 它是一种语言! 使用这种语言可以在自己的脚本中,添加一些规则限制,这样脚本在执行 ...