【发布时间】:2022-09-23 23:19:57
【问题描述】:
我在用交叉口观察者 API对检测到视口上的元素做出反应。但我也希望元素应在视口上保持至少 3 秒,然后在此之后检测到。我该怎么做?下面给出的是我正在使用的代码。
const callBack = (entries) => {
const [entry] = entries;
if (entry.isIntersecting) {
console.log(\"intersecting\");
}
};
const options = {
root: null,
rootMargin: \"0px\",
threshold: 0.75,
};
useEffect(() => {
const observer = new IntersectionObserver(callBack, options);
if (cardRef.current) {
observer.observe(cardRef.current);
}
return () => {
if (cardRef.current) {
observer.unobserve(cardRef.current);
}
};
}, []);
标签: reactjs user-interface frontend intersection-observer react-intersection-observer