작업하려는 페이지는 커버 이미지(절반 위)와 본문(절반 아래)으로 이루어졌다.
상단 또는 하단으로 스크롤되어 포커싱하도록 구현하려고 한다.
// 하단 -> 상단
const downScrollRef = useRef<HTMLDivElement | null>(null);
// 상단 -> 하단
const UpScrollRef = useRef<HTMLDivElement | null>(null);
const [contentClick, setContentClick] = useState<boolean>(false);
<article
className={`bg-red ${contentClick ? "h-[800px] pt-20" : "pt-8"} absolute top-[400px] min-h-[390px] px-5`}
ref={downScrollRef}
>
...
useEffect(() => {
if (contentClick) {
downScrollRef?.current?.scrollIntoView({ behavior: "smooth" });
}
}, [contentClick]);
<Layout>
<div ref={UpScrollRef}>
...
7-1. 만약 contentClick이 이미 false라면, 즉 화면이 상단으로 이동하지 않은 상태라면 contentClick을 true로 만들어 스크롤을 실행한다.
if (!contentClick) return setContentClick(true);
else {
UpScrollRef.current?.scrollIntoView({
behavior: "smooth",
block: "start",
});
...
}
setTimeout(() => {
setContentClick(false);
}, 400);
https://stackoverflow.com/questions/62497110/detect-scroll-direction-in-react-js