Modal창이 fixed면 스크롤이 있을 때 맨 상단으로 올라간다고 한다...
position도 바꿔보고, body의 overflow를 hidden으로도 해봤는데 그래도 계속 맨 위에서 떴다.
구글링해서 가져온 코드에서 조금(이라고 썼지만 오래 걸렸다..) 수정했다.
Modal 이 켜질 때, 없어질 때에 body.style과 scroll을 바꾸는 식이다.
const body = document.querySelector("body") as HTMLElement;
const [scrollPositions, setScrollPositions] = useState(window.scrollY);
window.addEventListener("scroll", () => {
if (window.scrollY !== 0) {
setScrollPositions(window.scrollY);
}
});
useEffect(() => {
if (Modal.isOn) {
console.log("켜져요");
body.style.overflow = "hidden";
body.style.position = "fixed";
body.style.top = `-${scrollPositions}px`;
console.log(body.style.top);
body.style.left = "0";
body.style.right = "0";
} else {
console.log("없어져요");
body.style.removeProperty("overflow");
body.style.removeProperty("position");
body.style.removeProperty("top");
body.style.removeProperty("left");
body.style.removeProperty("right");
window.scrollTo(0, scrollPositions);
}
return () => {};
}, [Modal.isOn]);
https://im-developer.tistory.com/201
iOS 디바이스에서 body의 scroll을 막는 방법 (How to prevent body scrolling on iOS?)
지난 주부터 회사에서 해결이 안되서 골머리를 앓았던 문제에 대해서 얘기해보려고 한다. 보통의 UI에서 대부분 모달(팝업이라고도 부른다)이 뜨면 모달 뒤에 body 영역을 반투명한 검정색 레이
im-developer.tistory.com
https://yuddomack.tistory.com/entry/Modal-%EB%A7%8C%EB%93%A4%EA%B8%B0body-scroll-lock
Modal 만들기(body scroll lock)
이번 글에서는 modal을 만들어보도록 하겠습니다. 1. 모달(modal)을 만들자 알고 계시겠지만 모달은 일종의 팝업창으로 웹 서비스에서 뭔가 삭제 버튼을 눌렀을 때, 주변이 흐려지면서 정말 삭제하
yuddomack.tistory.com
[javascript] 모달을 열 때 BODY가 스크롤되는 것을 방지 - 리뷰나라
내 웹 사이트 에서 Modal ( http://twitter.github.com/bootstrap )을 여는 동안 마우스 휠을 사용할 때 몸이 스크롤을 멈추기를 원합니다 . 모달이 열렸을 때 아래의 자바 스크립트를 호출하려고했지만 성공
daplus.net
'프론트엔드✏️ > 개인공부' 카테고리의 다른 글
[원티드] 프리온보딩 SPA Router 구현하기 (0) | 2022.10.07 |
---|---|
[Next.js] Next.js 설치 (1) | 2022.10.07 |
[css] 자릿수 계산하기,,, 고정되지 않은 width값 transition 주는 방법 알고 싶다. (0) | 2022.09.27 |
SSR vs CSR (1) | 2022.09.26 |
[styled-component] 별점 라이브러리 (0) | 2022.09.23 |