vue實現(xiàn)下拉加載其實沒那么復雜
前言
之前缺乏移動端的經(jīng)驗。一直不知道上拉加載,下拉刷新是怎么實現(xiàn)的?,F(xiàn)在正好有個產(chǎn)品有這樣一個需求。想了一會沒有思路。就去找插件。啥vue-infinite-scroll,vue-virtual-scroll-list。啊呀,牛!無限滾動,十萬條數(shù)據(jù)渲染。
經(jīng)過我一大圈的折騰。還是默默的卸載了插件。我只是需要實現(xiàn)一個下拉加載,不需要其他這么多的功能??戳丝雌渌说脑创a,直接擼了起來,實現(xiàn)一個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 滾動條離頂部的高度 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 { // 去除滾動條邊框 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é)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
相關文章
vue3中defineEmits與defineProps的用法實例
這篇文章主要介紹了vue3中defineEmits/defineProps的用法實例,需要的朋友可以參考下2023-12-12Element Table的row-class-name無效與動態(tài)高亮顯示選中行背景色
這篇文章主要介紹了Element Table的row-class-name無效與動態(tài)高亮顯示選中行背景色,本文詳細的介紹了解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11VUE父組件異步獲取數(shù)據(jù),子組件接收的值為空的問題
這篇文章主要介紹了VUE父組件異步獲取數(shù)據(jù),子組件接收的值為空的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10vue 查看dist文件里的結(jié)構(gòu)(多種方式)
本文通過三種方式給大家介紹了vue 查看dist文件里的結(jié)構(gòu),非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01