-- 두 요소의 가로 스크롤을 비율로 맞춘다. 서로 리스너를 건다
sync :: Box -> Box -> Ignore -> (IO (), Ignore)
sync source target ignore
| ignore == Just source = (pure (), Nothing) -- 내가 일으킨 이벤트 하나를 건너뛴다
| scrollLeft target == next = (pure (), ignore) -- 값이 같으면 이벤트도 안 온다 — 표시하면 다음 사용자 스크롤을 삼킨다
| otherwise = (scrollLeft target := next, Just target)
where ratio = scrollLeft source / (scrollWidth source - clientWidth source)
next = round (ratio * (scrollWidth target - clientWidth target))
-- 동기 플래그로는 못 막는다: scroll 이벤트는 넣는 순간이 아니라 다음 렌더링에 온다
isSyncing := True >> scrollLeft target := next >> isSyncing := False -- 이벤트가 올 때 이미 False
hash 링크로 연결될 경우 스크롤위치가 최상단으로 위치하기 때문에 문제(헤더가 고정일 경우)가 있을수도 있어서 scroll-margin-top으로 제어가 가능한 부분을 설명하고 있다.
- Add scroll margin to all elements which can be targeted - Piccalilli1
- Fixed Headers and Jump Links? The Solution is scroll-margin-top | CSS-Tricks
- Prevent content from being hidden underneath a fixed header by using scroll-margin-top – Bram.us
Footnotes
-
2ex유닛을 사용하여 선택한 글꼴의 x 높이의 상대적인 크기로 설정. ↩