let's get IT with DAVINA ๐Ÿ’ป

prevent scroll event with MODAL (์Šคํฌ๋กค ๋ฐฉ์ง€-๋ชจ๋‹ฌ์ฐฝ) ๋ณธ๋ฌธ

DEV_IN/React

prevent scroll event with MODAL (์Šคํฌ๋กค ๋ฐฉ์ง€-๋ชจ๋‹ฌ์ฐฝ)

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

์™œ ๊ฐ€? ํ”„๋กœ์ ํŠธ์— ์ ์—ˆ๋˜ ์Šคํฌ๋กค๋ง ๋ฐฉ์ง€์— ๋Œ€ํ•ด ์—ฌ๋Ÿฌ ๊ฒ€์ƒ‰๊ณผ ์ง€์ธ์—๊ฒŒ ๋ฌผ์–ด๋ณธ ๊ฒฐ๊ณผ.. ์ข€ ๋” ํšจ์œจ์ ์ด๊ณ  ์ง€์ €๋ถ„ํ•˜์ง€ ์•Š์€ ์ฝ”๋“œ๋ฅผ ์•Œ์•„๋ƒˆ๋‹ค.

 

๋ชจ๋‹ฌ์ฐฝ์„ ๋„์› ์„ ๋•Œ, ์Šคํฌ๋กค ๋ฐฉ์ง€๋ฅผ ํ•˜๊ณ  -> ๋ชจ๋‹ฌ์ฐฝ์„ ๋‹ซ์œผ๋ฉด ๋‹ค์‹œ ์Šคํฌ๋กค ํ™œ์„ฑํ™”๋ฅผ ์‹œํ‚ค๋ ค๋ฉด

overflow:hidden ์†์„ฑ์„ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค!

 

  • modal์ฐฝ์˜ ์ƒํƒœ๋ฅผ useState๋กœ ์ƒํƒœ๊ฐ’์„ ์ง€์ผœ๋ณด๊ณ 
  • ๋ชจ๋‹ฌ์ฐฝ์ด ๋œจ๋ฉด, scroll ์ƒํƒœ๋ฅผ hidden์œผ๋กœ ๋น„ํ™œ์„ฑํ™”์‹œํ‚ค๊ณ 
  • ๋ชจ๋‹ฌ์ฐฝ์„ ๋‹ซ์œผ๋ฉด, hidden์„ ๋‹ค์‹œ ํ’€์–ด์ฃผ๋Š” unset์†์„ฑ์„ ์ค˜์„œ ์Šคํฌ๋กค์„ ๋‹ค์‹œ ํ™œ์„ฑํ™”์‹œํ‚จ๋‹ค.
const [modal, setModal] = useState(false);

const handleModal = () => {
	if (modal === false) {
		setModal(true);
		document.body.style.overflow = "hidden";
	} else {
		setModal(false);
		document.body.style.overflow = "unset";
}

 

 

Comments