메뉴 스크롤 인덱스 계산 로직 개선
- getCurrentCategoryIndex 함수 로직 단순화 - 스크롤 방향 구분 없이 일관된 로직 적용 - 뷰포트 상단 기준점으로 카테고리 영역 판단 - 인덱스 1 오차 문제 해결 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -147,48 +147,30 @@ export const Menu = ({
|
|||||||
const containerStyle = window.getComputedStyle(scrollRef.current);
|
const containerStyle = window.getComputedStyle(scrollRef.current);
|
||||||
const paddingTop = parseFloat(containerStyle.paddingTop) || 0;
|
const paddingTop = parseFloat(containerStyle.paddingTop) || 0;
|
||||||
const adjustedScrollTop = scrollTop + paddingTop;
|
const adjustedScrollTop = scrollTop + paddingTop;
|
||||||
const isScrollingUp = scrollTop < lastScrollTop.current;
|
|
||||||
|
|
||||||
if (isScrollingUp) {
|
// 뷰포트의 상단 기준점 (약간의 오프셋 추가)
|
||||||
// 스크롤 업: 화면 상단 기준으로 카테고리 선택
|
const viewportTopWithOffset = adjustedScrollTop + 50;
|
||||||
const checkPoint = scrollTop + paddingTop + SCROLL_UP_OFFSET_PX;
|
|
||||||
|
|
||||||
for (let i = buttonRefs.current.length - 1; i >= 0; i--) {
|
// 현재 뷰포트에 가장 많이 보이는 카테고리 찾기
|
||||||
|
for (let i = 0; i < buttonRefs.current.length; i++) {
|
||||||
const element = buttonRefs.current[i];
|
const element = buttonRefs.current[i];
|
||||||
if (!element) continue;
|
if (!element) continue;
|
||||||
|
|
||||||
const currentTop = element.offsetTop;
|
const currentTop = element.offsetTop;
|
||||||
|
|
||||||
if (checkPoint >= currentTop) {
|
// 마지막 카테고리인 경우
|
||||||
if (i < buttonRefs.current.length - 1) {
|
if (i === buttonRefs.current.length - 1) {
|
||||||
const nextElement = buttonRefs.current[i + 1];
|
if (viewportTopWithOffset >= currentTop) {
|
||||||
if (nextElement && checkPoint >= nextElement.offsetTop) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 스크롤 다운: 카테고리 시작점 기준
|
// 다음 카테고리가 있는 경우
|
||||||
for (let i = buttonRefs.current.length - 1; i >= 0; i--) {
|
|
||||||
const element = buttonRefs.current[i];
|
|
||||||
if (!element) continue;
|
|
||||||
|
|
||||||
const currentTop = element.offsetTop;
|
|
||||||
|
|
||||||
if (adjustedScrollTop >= currentTop) {
|
|
||||||
if (i === buttonRefs.current.length - 1) {
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
const nextElement = buttonRefs.current[i + 1];
|
const nextElement = buttonRefs.current[i + 1];
|
||||||
if (nextElement) {
|
if (nextElement) {
|
||||||
const nextTop = nextElement.offsetTop;
|
const nextTop = nextElement.offsetTop;
|
||||||
if (adjustedScrollTop < nextTop) {
|
|
||||||
return i;
|
// 현재 카테고리 영역 내에 뷰포트 상단이 있으면 선택
|
||||||
}
|
if (viewportTopWithOffset >= currentTop && viewportTopWithOffset < nextTop) {
|
||||||
} else {
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user