let's get IT with DAVINA ๐Ÿ’ป

scroll prevent ๋ณธ๋ฌธ

Project/์™œ ๊ฐ€?

scroll prevent

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 2. 17. 19:51

ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋ฉฐ ๋ชจ๋‹ฌ์ฐฝ์„ ๋„์šธ ๋•Œ, spacebar์„ ๋ˆ„๋ฅผ๋•Œ๋งˆ๋‹ค ์ž๊พธ ์Šคํฌ๋กค์ด ๋ฐœ์ƒํ•˜์—ฌ ๋ถˆํŽธํ•ด์„œ 

๋ชจ๋‹ฌ์ฐฝ์ด ๋œจ๋ฉด ์Šคํฌ๋กค ์ž์ฒด๊ฐ€ ์•ˆ๋˜๊ฒŒ ๋ฐฉ์ง€ํ•˜๋„๋ก ๊ตฌํ˜„ํ–ˆ๋‹ค.

 

  • utils/useScrollPrevent.jsx
    • ์—ฌ๋Ÿฌ ํŽ˜์ด์ง€์— ์“ฐ์ผ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด๋ฏ€๋กœ util์— ๋”ฐ๋กœ ๋นผ์ฃผ์ž
import { useEffect } from "react";

const useScrollPrevent = () => {
  useEffect(() => {
    document.body.style.cssText = `
          position: fixed; 
          top: -${window.scrollY}px;
          overflow-y: scroll;
          width: 100%;`;
    return () => {
      const scrollY = document.body.style.top;
      document.body.style.cssText = "";
      window.scrollTo(0, parseInt(scrollY || "0", 10) * -1);
    };
  }, []);
  return;
};

export default useScrollPrevent;
  • Components/LoginModal.jsx
    • ์›ํ•˜๋Š” ํŽ˜์ด์ง€ ๋ฐ ์ปดํฌ๋„ŒํŠธ๋งˆ๋‹ค ํ•„์š”ํ•  ๋•Œ ๋ถˆ๋Ÿฌ์™€์„œ ์ ์šฉ์‹œํ‚ค๊ธฐ
import useScrollPrevent from "../utils/useScrollPrevent";

export default function LoginModal({ isModal }) {
    useScrollPrevent();
}

 

โ“โ“โ“
JS์—์„œ ์“ฐ์ด๋Š” spacebar๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ, ์Šคํฌ๋กค ์ด๋ฒคํŠธ ๋ฐฉ์ง€ ๋ฐฉ๋ฒ•์„ ์“ฐ๋ ค ํ–ˆ์„ ๋•Œ, ์ ์šฉ์ด ์•ˆ๋๋‹ค.. 
๋ฌผ๋ก  react์—์„œ ๊ทธ๋Œ€๋กœ ์“ธ ์ˆœ ์—†๊ฒ ์ง€๋งŒ.. ์•„์˜ˆ ์ƒ๊ธฐ ๋ฐฉ๋ฒ•์ฒ˜๋Ÿผ ์Šคํฌ๋กค ์ž์ฒด๋ฅผ preventํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค JS์—์„œ์ฒ˜๋Ÿผ spacebar๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ์—๋งŒ ์Šคํฌ๋กค ์ด๋ฒคํŠธ๋ฅผ ๋ฐฉ์ง€์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ผ๊นŒ๋‚˜..
์•„๋Š” ๋ถ„ ๊ณ„์‹œ๋ฉด teach me plz..๐Ÿฅบ

 

'Project > ์™œ ๊ฐ€?' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

ํ”„๋กœ์ ํŠธ ๋ถ„์„  (1) 2023.03.24
ํ†ต์‹  test  (2) 2023.02.21
๋‘ ๋‚ ์งœ ์‚ฌ์ด ๊ณ„์‚ฐ  (0) 2023.02.09
KakaoMap Api ์ ์šฉ  (0) 2023.02.09
button count ์กฐ์ •/์ œํ•œ  (0) 2023.02.09
Comments