项目需要用到上拉刷新下拉加载 所以自己手动实现了一个 组件:

<template><div class="my-scroll" :class="[scrollState?'prohibit':'allow']" ref="myScroll" @scroll.passive="onScroll($event)" @touchstart="touchStart($event)" @touchmove="touchMove($event)" @touchend="touchEnd($event)"  ><div class="scroll-top" :style="{height:top+'px'}"><div v-if="aspect==2"><p v-if="state==6">下拉刷新</p><p v-if="state==1"><i><img :src="Load"/></i><br/>刷新中</p><p v-if="state==2">松开刷新</p><p v-if="state==3"><i><img :src="Load"/></i><br/>刷新完成</p></div></div><!-- top --><div class="scroll-list" :style="{ transform: 'translate3d(0, ' + top + 'px, 0)'}"><slot name='scrollList'></slot><div class="scroll-bottom"><div v-if="state==4">加载中</div><div v-if="state==5">加载完成</div><div v-if="state==7">没有更多</div></div></div></div>
</template>
<script type="text/javascript">
import tween from '@/plugins/tween'
import Load from '@/assets/Load.gif'export default {name:'myScroll',props:{'page':{type:Object,  //counter:当前页  pageStart:开始页数  pageEnd:结束页数  total:总页数},'onRefresh':{ //刷新回调type:Function,require:true},'onPull':{ //加载回调type:Function,require:true},'getScrollTop':{ //获取滚动条位置type:Function},'setScrollPage':{ //改变滚动条位置type:Function},'scrollState':{//是否可滑动type:Boolean,require:true}},data(){return {Load,pageX:0,pageY:0,state:0, scrollPosition:0,myScroll:null,myScrollList:null,top:0,aspect:0, //1:向下 2:向上listHeight:0,}},created(){},methods:{ScrollTop(top){ //修改滚动条位置this.myScroll.scrollTop = top},/** 刷新中:1* 松开刷新:2* 刷新完成:3* 加载中:4* 加载完成:5* 下拉刷新:6* 没有更多:7*/setState(index){ //修改状态this.state = indexif(index == 5||index == 3){setTimeout(()=>{this.state = 0let timer = null;let that = this;var b=50,c=100,d=100,t=0;cancelAnimationFrame(timer);timer = requestAnimationFrame(function fn() {var oTop = that.top;if (that.top>0) {that.top = Math.ceil(tween.Quart.easeInOut(10,oTop,8,10)) - 15timer = requestAnimationFrame(fn);} else {cancelAnimationFrame(timer);that.top = 0}});},500)}},touchStart(e){ //触摸事件this.pageX = e.targetTouches[0].pageXthis.pageY = e.targetTouches[0].pageY},touchMove(e){ //触摸滑动事件this.scrollPosition = this.myScroll.scrollTop //获取滚动条位置if(this.scrollState&&e.targetTouches[0].pageY>this.pageY){ //向上滑动this.aspect = 2if(this.myScroll.scrollTop==0){let diff = e.targetTouches[0].pageY - this.pageY - this.scrollPositionthis.top = Math.pow(diff, 0.9)let ranget = diff/document.body.clientHeight*100 //计算在屏幕上滑动了多少if(ranget > 20){this.state = 2}else if(ranget < 15){this.state = 6}e.preventDefault()}}else if(this.scrollState&&this.state!=4){ //向上滑动this.aspect = 1}},touchEnd(e){if(this.aspect == 2&&this.state == 2||this.state == 1){ //上拉处理this.top = 100this.state = 1this.topCallback()}else if(this.aspect == 2){this.state = 0this.top = 0}},onScroll(e){let listHeight = this.myScrollList.offsetHeight //列表总高度let listScrollTop = e.target.scrollTop + this.myScroll.offsetHeight //当前滚动条位置if(this.state == 0&&listHeight-listScrollTop < 100){this.bottomCallback()}if(this.getScrollTop)this.getScrollTop(e.target.scrollTop) //返回X,Y},topCallback(){ //刷新回调this.onRefresh(this.state)},bottomCallback(){ //加载回调if(this.state != 7){this.state = 4this.onPull(this.state)}},},mounted(){this.myScroll = this.$refs.myScroll //获取滑条domthis.myScrollList = this.myScroll.children[1] //获取列表dom}}
</script>
<style lang="scss" scoped>.allow{overflow:hidden;height: auto;}.prohibit{max-width: 100%;max-height: 100%;height: 100%;overflow:hidden;overflow-y: scroll;-webkit-overflow-scrolling: touch;will-change: transform;transition: all 450ms;backface-visibility: hidden;perspective: 1000;}.my-scroll{position: relative;color: #fff;.scroll-top{text-align: center;display:flex;position:absolute;top:0;left:0;width:100%;overflow: hidden;div{display:flex;height:auto;width:100%;justify-content: center;align-items:center;flex-wrap: wrap;i{flex:1 0 100%;display:block;height: 0.4rem;}img{width:0.6rem;}p{flex:1 0 100%;}}}.scroll-list{overflow:hidden;min-height: 100%;}.scroll-bottom{text-align: center;line-height: 40px;}}
</style>
复制代码

使用:

<template><div class="index"><my-scroll  ref="myScroll" :page="page" :on-refresh="onRefresh" :on-pull="onPull"  :get-scroll-top="getTop"><div slot="scrollList"><ul><li v-for="(x,index) in list" :key="index">列表</li></ul></div></my-scroll></div>
</template>
<script type="text/javascript">import myScroll from '@/components/myScroll.vue'export default {data(){return{list:[],page:{counter:1,  pageStart:1,  pageEnd:1,  total:10},}},methods:{onRefresh(mun){ //刷新回调setTimeout(()=>{this.$refs.myScroll.setState(3);},500)},onPull(mun){ //加载回调if(this.page.counter<=this.page.total){setTimeout(()=>{this.page.counter++this.$refs.myScroll.setState(5)for(let i=0;i<10;i++){this.listdata.push({})}},500)}else{this.$refs.myScroll.setState(7)}},getTop(y) {//滚动条位置},},components:{myScroll},created(){},mounted(){for(let i=0;i<1*50;i++){this.list.push({})}},}
</script>
<style lang="scss" scoped>.index{}
</style>
复制代码

上拉刷新实现思路: 1.通过touchstart获取用户第一次点击的坐标 2.通过touchmove 判断向上滑动还是向下 4.判断列表的滚动条是否在最顶部 5.然后判断在这个屏幕滑动的比例 进行状态显示 下拉加载实现思路: 1.通过判断滚动条位置实现下拉加载

样式:

注:缓动算法与tween.js 来自 www.cnblogs.com/cloudgamer/… 详细用法这里都有

github地址:github.com/FZliweilian…

转载于:https://juejin.im/post/5b836fdff265da4379348fc9

vue 实现上拉加载下拉刷新(思路贼清晰)相关推荐

  1. php微信小程序向下滑动,微信小程序功能实现:上滑加载下拉刷新

    本篇文章给大家带来的内容是关于微信小程序功能实现:上滑加载下拉刷新,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 之前谈到文章列表的数据加载,是一次性全部加载,这样是不友好的.这章介 ...

  2. ionic上拉加载-下拉刷新

    ionic上拉加载-下拉刷新 1.上拉加载 <ion-infinite-scroll on-infinite="loadOlderStories()" distance=&q ...

  3. Mint-ui中loadmore(上拉加载下拉刷新)组件在ios中滑动会触发点击事件的解决方法...

    bug说明: Mint-ui中loadmore(上拉加载下拉刷新)组件 在 使用fastclick的情况下 ,在ios设备中滑动会触发点击事件: 解决方法: 我是按需引入,去项目中找到loadmore ...

  4. php mescroll,mescroll.js上拉加载下拉刷新组件使用详解

    本文实例为大家分享了上拉加载下拉刷新组件mescroll.js的具体代码,供大家参考,具体内容如下 使用注意事项: 1.引入的时候出问题及时看官方给出的解决方案(基本上都必须看): 2.react中一 ...

  5. 教你如何使用SwipeRefreshLayout来构建一个上拉加载下拉刷新框架

    前言: 基本上所以的移动端应用都有Listview(当然RecyclerView也一样),那必不可少的都会嵌入一个上拉加载下拉刷新的功能.这样能大大的减少用户的流量消耗,同样对于用户也有更好的用户体验 ...

  6. ios 上拉加载 下拉刷新

    在一款 App应用中有的时候会用到上拉加载下拉刷新的功能,本人觉得SVPullToRefresh很好用(可以用在UIScrollView上,包括UITableview和UICollectionView ...

  7. vue 实现上拉加载下拉刷新(思路清晰)

    项目需要用到上拉刷新下拉加载 所以自己手动实现了一个 组件: <template><div class="my-scroll" :class="[scr ...

  8. uniapp 自定义上拉加载下拉刷新组件

    介绍 该组件是结合uview框架写的,主要结合了里面的u-loadmore组件,可配置下拉刷新加载圈的颜色及背景色,暂无数据时的图等,突出的特点就是通过设置组件的高度,适配刘海屏iPhone,且支持嵌 ...

  9. 上拉加载下拉刷新了解下

    2019独角兽企业重金招聘Python工程师标准>>> 老样子,我们先,哦不,今天我们直接上思路,没有效果图,真的没有 我们依旧从界面及逻辑两块进行分析 1.界面上,只分成简单的两块 ...

最新文章

  1. 【教程】【FLEX】#004 反射机制
  2. kubernetes入门指南(一)
  3. dedecms首页如何调用专题页描述和链接
  4. TZOJ上的C语言作业答案,C语言编程练习
  5. STM32 LWIP TCP 数据包分包后合并,超过591字节后分会分包问题
  6. 3 Curator框架实现分布式锁
  7. 探索7.x, 全面解析Activity启动框架 (1)
  8. 吐血整理!12种通用知识图谱项目简介
  9. 前端抓潜,必须设置门槛,筛选优质客户
  10. jQuery Howto: 如何快速创建一个AJAX的加载的图片效果
  11. android学习笔记1-布局的初步了解以及 控件的常见属性
  12. java day40【会话技术:Cookie 、会话技术:Session 、JSP:入门学习】
  13. UE5热更新(Pak包的Cook,打包,加载,踩过的一些坑)
  14. ASP.Net Ajax框架 Timer控件 计时器
  15. xvideos打开显示服务器出错,为什么打开xvideo显示网页
  16. ssdp java_SSDP协议的Android实现以及使用
  17. paraview远程模式
  18. 图片处理--处理成灰色图片,高斯模糊
  19. Linux 虚拟内存管理思维导图
  20. Linux 命令(179)—— nice 命令

热门文章

  1. AWVS14.1安装
  2. 背包问题不同要求下的初始化
  3. 联想手机X2受到爱马仕继承人侯诺热赞
  4. Win10+Cuda10.2的Pytorch安装
  5. csp-202203
  6. 数字图像处理之灰度化
  7. 计算机对康复治疗学的帮助,我院康复医学科引进认知训练配合计算机心理失语症脑功能评估训练系统...
  8. Mybatis最入门---分页查询(逻辑分页与SQL语句分页)
  9. Spring框架核心思想
  10. Tomcat脚本一键装机