vue實(shí)現(xiàn)下拉加載其實(shí)沒(méi)那么復(fù)雜
前言
之前缺乏移動(dòng)端的經(jīng)驗(yàn)。一直不知道上拉加載,下拉刷新是怎么實(shí)現(xiàn)的?,F(xiàn)在正好有個(gè)產(chǎn)品有這樣一個(gè)需求。想了一會(huì)沒(méi)有思路。就去找插件。啥vue-infinite-scroll,vue-virtual-scroll-list。啊呀,牛!無(wú)限滾動(dòng),十萬(wàn)條數(shù)據(jù)渲染。
經(jīng)過(guò)我一大圈的折騰。還是默默的卸載了插件。我只是需要實(shí)現(xiàn)一個(gè)下拉加載,不需要其他這么多的功能??戳丝雌渌说脑创a,直接擼了起來(lái),實(shí)現(xiàn)一個(gè)List組件。
效果展示

MList.vue
<template>
<div class="list-wrap">
<div class="content" ref="list" @scroll="onScroll">
<slot></slot>
</div>
<div class="loading" v-show="loading">正在加載數(shù)據(jù)......</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 滾動(dòng)條離頂部的高度 scrollHeight 列表內(nèi)容的高度
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 { // 去除滾動(dòng)條邊框
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。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Vue實(shí)現(xiàn)淘寶購(gòu)物車三級(jí)選中功能詳解
這篇文章主要介紹了通過(guò)Vue實(shí)現(xiàn)淘寶購(gòu)物車中三級(jí)選中的功能,文中的實(shí)現(xiàn)過(guò)程講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定的幫助,感興趣的可以了解一下2022-01-01
vue3中defineEmits與defineProps的用法實(shí)例
這篇文章主要介紹了vue3中defineEmits/defineProps的用法實(shí)例,需要的朋友可以參考下2023-12-12
Vue-CLI3.x 自動(dòng)部署項(xiàng)目至服務(wù)器的方法步驟
本教程講解的是 Vue-CLI 3.x 腳手架搭建的vue項(xiàng)目, 利用scp2自動(dòng)化部署到靜態(tài)文件服務(wù)器 Nginx,感興趣的可以了解一下2021-11-11
vue項(xiàng)目每30秒刷新1次接口的實(shí)現(xiàn)方法
在vue.js項(xiàng)目中,經(jīng)常需要對(duì)數(shù)據(jù)實(shí)時(shí)更新——每隔xx秒需要刷新一次接口——即需要用到定時(shí)器相關(guān)原理。這篇文章主要介紹了vue項(xiàng)目每30秒刷新1次接口的實(shí)現(xiàn)方法,需要的朋友可以參考下2018-12-12
Element Table的row-class-name無(wú)效與動(dòng)態(tài)高亮顯示選中行背景色
這篇文章主要介紹了Element Table的row-class-name無(wú)效與動(dòng)態(tài)高亮顯示選中行背景色,本文詳細(xì)的介紹了解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
VUE父組件異步獲取數(shù)據(jù),子組件接收的值為空的問(wèn)題
這篇文章主要介紹了VUE父組件異步獲取數(shù)據(jù),子組件接收的值為空的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue 查看dist文件里的結(jié)構(gòu)(多種方式)
本文通過(guò)三種方式給大家介紹了vue 查看dist文件里的結(jié)構(gòu),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01

