better-scroll实现上下拉加载效果

vue 使用better-scroll实现上下拉加载效果

安装插件
npm install better-scroll --save

页面使用

<script>
	import Bscroll from "better-scroll";
	export default {
	  data() {
		    return {
		    	isPullDown: false,
			      pageNo: 1, //当前页
			      pageSize: 20, //每页显示
			      totalPage: "", //总页数
			      totalNo: "", //总条数
			      options: {
			        click: true,//解决页面点击事件不触发
			        scrollY: true,
			        pullDownRefresh: {
			          threshold: 30, //下拉距离超过30px触发pullingDown事件
			          stop: 20 / 回弹停留在距离顶部20px的位置
			        }
			      }
		    }
	   },
	   created() {
		    this.list();
		  },
	    mounted() {
	    	this.$nextTick(() => {
		      this.initScroll();
		    });
		},
		methods: {
			initScroll() {
		      this.myScroll = new Bscroll(".box",this.options);
		      // this.myScroll = new Bscroll(".mytask_main", { mouseWheel: true, click: true, tap: true });
		      // 上拉加载
		      this.pullDownEvent();
		      // 下拉刷新
		      this.pullUpLoadEvent();
		    },
		    finishPulling() {
		      this.myScroll.finishPullDown(); // 结束下拉刷新
		      this.myScroll.finishPullUp(); // 结束上拉加载更多
		      this.myScroll.refresh(); // dom节点变化,重新计算better-scroll
		    },
		    pullDownEvent() {
		      this.myScroll.on("pullingDown", () => {
		        this.isPullDown = true;
		        console.log("下拉刷新");
		        this.pageNo = 1;
		        this.getList();
		      });
		    },
		    pullUpLoadEvent() {
		      this.myScroll.on("pullingUp", () => {
		        console.log("上拉加载");
		        if (this.pageNo < this.totalPage) {
		          this.pageNo++;
		          this.getList();
		        } else {
		          this.isPullDown = false;
		        }
		      });
		    },
		    getList(){
		   		 let _this=this
		    	let data={
		    	//请求的参数
		    	}
		    	reqUrl(this,data).then(res=>{
		    		_this.pageNo = res.body.pageNo;
            		_this.totalPage = res.body.totalPage;
						if (_this.pageNo != 1) {
			             // 不是第一页,要拼接上一页的数据
			              _this.list = _this.list.concat(res.body.list);
			              if (_this.pageNo != res.body.totalPage) {
			                _this.isPullDown = true;
			              }
			            } else {
			              // 第一页
			              _this.list = res.body.list;
			            }
			            _this.$nextTick(() => {
			              _this.finishPulling();
			            });
		    	}).catch(err=>{
		    		console.log(err)
		    	})
		    }
		}
   }
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值