9433cfb9创建于 2025年12月31日历史提交
<template>
	<view>
		<page-head :title="title"></page-head>
		<view class="uni-padding-wrap uni-common-mt">
			<view class="uni-title uni-common-mt">
				{{appear ? '小球出现' : '小球消失'}}
			</view>
			<scroll-view class="scroll-view" scroll-y :scrollTop="scrollTop">
				<view class="scroll-area">
					<text class="notice">向下滚动让小球出现</text>
					<view class="ball"></view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>
<script lang="uts">
  let observer: IntersectionObserver | null = null;
	export default {
		data() {
			return {
				appear: false,
				title:'intersectionObserver',
				// 自动化测试
				testRes: null as ObserveResult | null,
				scrollTop:0
			}
		},
		onReady() {
			observer = uni.createIntersectionObserver(this,{});
			observer.relativeTo('.scroll-view').observe('.ball', (res:ObserveResult) => {
				this.testRes = res;
				if (res.intersectionRatio > 0 && !this.appear) {
					this.appear = true;
				} else if (res.intersectionRatio <= 0 && this.appear) {
					this.appear = false;
				}
			})
		},
		onUnload() {
			if (observer) {
				observer.disconnect()
			}
		}
	}
</script>
<style>
	.scroll-view {
		height: 200px;
		background: #fff;
		border: 1px solid #ccc;
		box-sizing: border-box;
	}

	.scroll-area {
		height: 650px;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.notice {
		margin-top: 75px;
		margin:75px 0 200px 0;
	}

	.ball {
		width: 100px;
		height: 100px;
		background: #4cd964;
		border-radius: 100px;
	}
</style>