vue封装无限滚动组件
- 组件接收两个props
· loading 是否加载
· finished 是否加载完成 - 组件接收一个自定义事件infinite,在该事件中触发发送请求
- 利用vueuse提供的useIntersectionObserver方法判断是否进入可视区,也可以自己利用IntersectionObserver自己封装是否进入可视区的方法。
<template>
<div class="xtx-infinite-loading" ref="target">
<div class="loading" v-if="loading">
<span class="img"></span>
<span class="text">正在加载...</span>
</div>
<div class="none" v-if="finished">
<span class="img"></span>
<span class="text">亲,没有更多了</span>
</div>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import { useIntersectionObserver } from "@vueuse/core";
interface Props {
loading:boolean,
finished:boolean
}
const props = defineProps<Props>()
const emits = defineEmits(['infinite'])
const target = ref()
const {stop} = useIntersectionObserver(target,([{isIntersecting}])=> {
if(isIntersecting) {
if(!props.loading&&!props.finished) {
emits('infinite')
}
}
},{
threshold: 0
})
</script>
<style scoped lang='less'>
.xtx-infinite-loading {
.loading {
display: flex;
align-items: center;
justify-content: center;
height: 200px;
.img {
width: 50px;
height: 50px;
background: url(../../assets/images/load.gif) no-repeat center / contain;
}
.text {
color: #999;
font-size: 16px;
}
}
.none {
display: flex;
align-items: center;
justify-content: center;
height: 200px;
.img {
width: 200px;
height: 134px;
background: url(../../assets/images/none.png) no-repeat center / contain;
}
.text {
color: #999;
font-size: 16px;
}
}
}
</style>