首页 / VUE / vue实现下拉加载其实没那么复杂
vue实现下拉加载其实没那么复杂
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了vue实现下拉加载其实没那么复杂,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1925字,纯文字阅读大概需要3分钟。
内容图文
前言
之前缺乏移动端的经验。一直不知道上拉加载,下拉刷新是怎么实现的。现在正好有个产品有这样一个需求。想了一会没有思路。就去找插件。啥vue-infinite-scroll,vue-virtual-scroll-list。啊呀,牛!无限滚动,十万条数据渲染。
经过我一大圈的折腾。还是默默的卸载了插件。我只是需要实现一个下拉加载,不需要其他这么多的功能。看了看其他人的源码,直接撸了起来,实现一个List组件。
效果展示
MList.vue
<template> <div class="list-wrap"> <div class="content" ref="list" @scroll="onScroll"> <slot></slot> </div> <div class="loading" v-show="loading">正在加载数据......</div> </div> </template> <script lang='ts'> import { Component, Vue, Watch, Prop } from "vue-property-decorator"; @Component({ components: {} }) export default class extends Vue { @Prop() private loading!: boolean; private onScroll() { const obj: any = this.$refs.list; // clientHeight 视口高度 scrollTop 滚动条离顶部的高度 scrollHeight 列表内容的高度 if (obj.clientHeight + obj.scrollTop === obj.scrollHeight) { this.$emit("toBottom"); } } } </script> <style scoped lang="scss"> .list-wrap { width: 100%; height: 100%; position: relative; .content { width: 100%; height: 100%; overflow-y: scroll; } .loading { position: absolute; bottom: -20px; width: 100%; height: 20px; color: #ffffff; } } ::-webkit-scrollbar { // 去除滚动条边框 width: 0 !important; } ::-webkit-scrollbar { width: 0 !important; height: 0; } </style>
使用组件
<div class="body"> <m-list @toBottom="fetchNewData()" :loading="loading"> <code-info class="item" v-for="(item,index) in dataList" :key="index"></code-info> </m-list> </div> private dataList: any[] = [1, 2, 3, 4, 5, 6, 7, 8]; private loading: boolean = false; private fetchNewData() { this.loading = true; setTimeout(() => { this.dataList.push(1, 2, 3); const ref: any = this.$refs.vueLoad; this.loading = false; }, 1000); }
这里需要注意的是m-list的父容器一定要固定高度,本例为body。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。
内容总结
以上是互联网集市为您收集整理的vue实现下拉加载其实没那么复杂全部内容,希望文章能够帮你解决vue实现下拉加载其实没那么复杂所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。