前言

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

直接上代码吧,哈哈

组件里:

 <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. AttributeError: module 'html.parser' has no attribute 'HTMLParseError'

    别人说这种错误大多是,因为beautifulsoup的版本兼容问题, 因为beautifulsoup的4.0以下版本和4.0版本与python3.5以上都不兼容, 网上的解决方案大多是:降python ...

  2. pandas 分组统计

    # coding:utf-8 import pandas as pd import numpy as np # path = r'C:\Users\wuzaipei\Desktop\桂林三金项目签到情 ...

  3. CreateThread()使用实例

    1.定义的全局变量 DWORD WINAPI ClientThread(LPVOID lpParam); struct ClientInfo {   SOCKET sock;   SOCKADDR_I ...

  4. MSF魔鬼训练营-3.3.2 口令猜测与嗅探

    密码暴力破解以SSH为例,其他协议方法类似 SSH      msf > use auxiliary/scanner/ssh/ssh_login msf auxiliary(ssh_login) ...

  5. MySQL数据类型 约束

    一.数据库CDGS. 库 增   create database 库名; 删   drop 库名; 改 alter database 库名称 修改的属性名称; 查 show databases;#查看 ...

  6. 在Ubuntu上安装Spark

    1.下载spark2.4.3 使用用户的hadoop的版本,解压并放到/usr/local下并改名为spark目录 2.设置spark目录为本用户所有 3.设置环境变量 (1)#~/.bashrc e ...

  7. Spring添加声明式事务

    一.前言 Spring提供了声明式事务处理机制,它基于AOP实现,无须编写任何事务管理代码,所有的工作全在配置文件中完成. 二.声明式事务的XML配置方式 为业务方法配置事务切面,需要用到tx和aop ...

  8. 03:linux文件操作四剑客

    1.1 find查找命令 1.find命令说明 1. Linux find命令用来在指定目录下查找文件. 2. 任何位于参数之前的字符串都将被视为欲查找的目录名. 3. 如果使用该命令时,不设置任何参 ...

  9. Django之ORM操作.md

    1.ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...

  10. 类的函数成员之属性property

    属性命名采用Pascal命名方式,每个单词的首字母大写.访问方式与访问类的公共字段类似. /// <summary> /// 字段 /// </summary> private ...