Skip to content

Commit

Permalink
fix: 스크롤시 메인 마진 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
LAIIonKING committed Nov 13, 2022
1 parent 94ecd9b commit ce14b47
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 0 additions & 1 deletion client/src/components/main/css/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ main{
display: flex;
justify-content: center;
align-items: center;
border: 1px solid black;
}
21 changes: 20 additions & 1 deletion client/src/components/main/js/Main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import '../css/main.scss';
import { Outlet } from 'react-router-dom';
import { useEffect, useState } from 'react';

export default function Main() {
const [fixNav, setFixNav] = useState({});
useEffect(() => {
window.addEventListener('scroll', handleScroll, { capture: true }); // 스크롤 이벤트 등록
return () => {
window.removeEventListener('scroll', handleScroll); // 스크롤 이벤트 등록 제거(성능저하방지)
};
}, []);

const handleScroll = () => {
if (scrollY > 120) {
setFixNav({
marginTop: '70px',
});
} else {
setFixNav({});
}
};

return (
<>
<main>
<main style={fixNav}>
<Outlet />
</main>
</>
Expand Down

0 comments on commit ce14b47

Please sign in to comment.