๐Ÿ˜ฎ ์Šคํฌ๋กค์— ๋ฐ˜์‘ํ•˜๋Š” NavBar

๋ฐ•์ƒ์€ยท2022๋…„ 5์›” 10์ผ
1

โœ๏ธ blelog โœ๏ธ

๋ชฉ๋ก ๋ณด๊ธฐ
11/13

๋Œ€๋ถ€๋ถ„์Šคํฌ๋กค ๋ฐฉํ–ฅ์— ๋ฐ˜์‘ํ•˜๋Š” ํ—ค๋”์—์„œ ์ฐธ๊ณ ํ•ด์„œ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

์Šคํฌ๋กค์„ ์•„๋ž˜๋กœ ๋‚ด๋ฆฌ๋ฉด ํ—ค๋”๊ฐ€ ์‚ฌ๋ผ์ง€๊ณ  ์œ„๋กœ ์˜ฌ๋ฆฌ๋ฉด ํ—ค๋”๊ฐ€ ๋‚˜ํƒ€๋‚˜๋„๋ก ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ํฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค.

โœ๏ธ ์ž‘์„ฑ ์ฝ”๋“œ

// 2022/05/11 - ์Šค๋กœํ‹€ ํ—ฌํผ ํ•จ์ˆ˜ - by 1-blue
export const throttleHelper = (callback: () => void, waitTime: number) => {
  let timerId: ReturnType<typeof setTimeout> | null = null;

  return () => {
    if (timerId) return;
    timerId = setTimeout(() => {
      callback();
      timerId = null;
    }, waitTime);
  };
};

// 2022/05/11 - ํ—ค๋” ์ˆจ๊น€ ์—ฌ๋ถ€ ๋ณ€์ˆ˜ - by 1-blue
const [hide, setHide] = useState(false);
// 2022/05/11 - ํ˜„์žฌ ์Šคํฌ๋กค ์œ„์น˜๊ฐ’ ์ €์žฅํ•  ๋ณ€์ˆ˜ - by 1-blue
const [pageY, setPageY] = useState(0);
// 2022/05/11 - ํ˜„์žฌ ์Šคํฌ๋กค์„ ๋‚ด๋ ธ๋Š”์ง€ ์˜ฌ๋ ธ๋Š”์ง€ ํ™•์ธํ•  ์Šคํฌ๋กค ์ด๋ฒคํŠธ ํ•จ์ˆ˜ - by 1-blue
const handleScroll = () => {
  const { pageYOffset } = window;
  const deltaY = pageYOffset - pageY;
  const hide = pageYOffset !== 0 && deltaY >= 0;
  setHide(hide);
  setPageY(pageYOffset);
};
// 2022/05/11 - ์Šคํฌ๋กค ์ด๋ฒคํŠธ์— ์Šค๋กœํ‹€๋ง ์ ์šฉ - by 1-blue
const throttleScroll = throttleHelper(handleScroll, 50);
// 2022/05/11 - ์Šคํฌ๋กค ์ด๋ฒคํŠธ ๋“ฑ๋ก - by 1-blue
useEffect(() => {
  document.addEventListener("scroll", throttleScroll);
  return () => document.removeEventListener("scroll", throttleScroll);
}, [throttleScroll]);

// jsx ( tailwindCss ์‚ฌ์šฉ )
<header
  className={combineClassNames(
    "sticky top-0 w-full bg-zinc-200 dark:bg-zinc-900 z-10 transition-transform duration-300",
    hide ? "-translate-y-20" : "translate-y-0"
  )}
>
  // ๋‚ด๋ถ€ ์ƒ๋žต...
</header>

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 1์›” 31์ผ

๋„์›€์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค ์ •๋ง ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ