我正在尝试创建具有粘性效果的标题,但是在滚动页面时出现此错误,有时它可以工作,有时会发生错误。谁能帮我解决这个问题?
const EventPage: React.FC = () => {
const [isSticky, setSticky] = useState(false);
const ref = useRef(null);
const handleScroll = () => {
if (ref.current.getBoundingClientRect().y <= -580 || null) {
console.log(ref.current.getBoundingClientRect().y);
setSticky(true);
} else {
setSticky(false);
}
};
useEffect(() => {
window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", () => handleScroll);
};
}, []);
return (
<div>
<Head>
<title>Event Page</title>
</Head>
<Header />
<div className={`sticky-wrapper${isSticky ? " sticky" : ""}`} ref={ref}>
{isSticky && <Sticky />}
</div>