Files
deerflow-code/situation-overview-web/src/components/scrollingloading/index.vue
T
2026-05-23 17:23:15 +08:00

71 lines
1.7 KiB
Vue

<template>
<div class="scrollingloading" ref="targetElement">
</div>
</template>
<script>
export default {
data() {
return {
isIntersecting: false,
targetElement: null,
observer: null
}
},
props: {
onState: {
type: Boolean,
default: false
},
},
methods: {
initObserver() {
this.targetElement = this.$refs.targetElement
this.observer = new IntersectionObserver(this.handleIntersection, {
threshold: 1 // 当元素尺寸进入视口时触发,0-1代表0%-100%
});
if (this.targetElement) {
this.observer.observe(this.targetElement);
}
},
stopObserver() {
if (this.targetElement && this.observer) {
this.observer.unobserve(this.targetElement);
}
},
handleIntersection(entries, observer) {
entries.forEach(entry => {
if (entry.isIntersecting) {
this.isIntersecting = true;
console.log(this.onState);
if (this.onState) {
this.$emit('onChange')
}
console.log('元素出现在了视图中,开始触发事件');
} else {
this.isIntersecting = false;
console.log('元素在视图中不可见');
}
});
}
},
mounted() {
this.initObserver();
console.log('初始化');
},
beforeDestroy() {
this.stopObserver();
},
}
</script>
<style lang="less">
.scrollingloading {
width: 100%;
height: 10px;
}
</style>