import { View, Image, Text } from '@tarojs/components'; import { useState, useEffect } from 'react'; import { useIntersectionObserver } from '@tarojs/taro'; interface LazyImageProps { src: string; placeholder?: string; className?: string; style?: React.CSSProperties; mode?: 'scaleToFill' | 'aspectFit' | 'aspectFill' | 'widthFix' | 'heightFix' | 'top' | 'bottom' | 'center' | 'left' | 'right' | 'top left' | 'top right' | 'bottom left' | 'bottom right'; } export default function LazyImage({ src, placeholder, className, style, mode = 'aspectFill' }: LazyImageProps) { const [loaded, setLoaded] = useState(false); const [inView, setInView] = useState(false); const { intersectionObserver } = useIntersectionObserver(); useEffect(() => { if (!intersectionObserver) return; intersectionObserver.relativeToViewport(); intersectionObserver.observe({ type: 'relativeToViewport', success: (res) => { if (res.intersectionRatio > 0) { setInView(true); } } }); return () => { intersectionObserver.disconnect(); }; }, [intersectionObserver]); return ( {!loaded && ( {placeholder ? ( ) : ( 加载中... )} )} {(inView || loaded) && ( setLoaded(true)} /> )} ); }