视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
vue实现下拉加载其实没那么复杂
2020-11-27 21:52:21 责编:小采
文档


前言

之前缺乏移动端的经验。一直不知道上拉加载,下拉刷新是怎么实现的。现在正好有个产品有这样一个需求。想了一会没有思路。就去找插件。啥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。

总结

下载本文
显示全文
专题