์คํฌ๋กค๋ฐ๋ฅผ ์ด๋ ๊ฐ๋งํผ ๋ด๋ฆฌ๋ฉด ํน์ ์๋ ๋จผํธ์ css ์์ฑ์ด ๋ณ๊ฒฝ๋์ด์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ์ข ์ข ์์ต๋๋ค.
์ฐ์ ํด๋น ํ๊ฒฝ์ ๋ง๋ค๊ธฐ ์ํด ๋งํฌ์ ์ ํฉ๋๋ค.
Window๊ฐ ์๋ div ์คํฌ๋กค์ธ ๊ฒฝ์ฐ
- App.js
<div className="App">
<div className="fixedBox">'I will be fixed! ๐'</div>
<div className="content" style={{fontSize: '22px'}}>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi at nihil sit fugit adipisci aperiam alias enim sed voluptates quo! Quam inventore officia commodi laudantium amet in sunt est quasi. <br />
Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi at nihil sit fugit adipisci aperiam alias enim sed voluptates quo! Quam inventore officia commodi laudantium amet in sunt est quasi. <br />
Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi at nihil sit fugit adipisci aperiam alias enim sed voluptates quo! Quam inventore officia commodi laudantium amet in sunt est quasi. <br />
... // ์คํฌ๋กค์ด ์๊ธธ๋๊น์ง ๋ณต๋ถ
</div>
</div>
- App.css
.fixedBox {
position: fixed;
top: 50px;
right: 20px;
z-index: 1;
padding: 10px;
background: lightseagreen;
border-radius: 6px;
color: #fff;
border: 1px solid rgb(23, 122, 117);
text-align: left;
}
์ด ์ํ์์ 300์ ๋ ์คํฌ๋กค์ ํ์๋ 'I will be fixed!' ๋ผ๋ ๋ฌธ๊ตฌ๋ฅผ 'I am fixed!' ๋ก ๋ฐ๊พธ๊ณ ,
์ต์๋จ์ ๊ณ ์ ํ๊ธฐ ์ํด top: 0 ๋ฅผ ์ฃผ๋๋ก ํ๊ฒ ์ต๋๋ค.
์ง์ css๋ฅผ ์ปจํธ๋กค ํ์ง ์๊ณ fixed๋ผ๋ ํด๋์ค๋ฅผ ๊ฐ์ง๊ณ ์ปจํธ๋กค์ ํ๊ฒ ์ต๋๋ค.
- App.css
{ ... }
.fixedBox.fixed {
width: 100%;
top: 0;
right: 0;
font-size: 22px;
border-radius: 0;
text-align: center;
}
- App.js
const [ScrollActive, setScrollActive] = useState(false);
<div className="App">
<div className={ScrollActive ? "fixedBox fixed" : "fixedBox"}>
{
ScrollActive ?
'I am fixed! ๐' :
'I will be fixed! ๐'
}
</div>
{/* <ElementScroll/> */}
<div className="content" style={{fontSize: '22px'}}>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi at nihil sit fugit adipisci aperiam alias enim sed voluptates quo! Quam inventore officia commodi laudantium amet in sunt est quasi. <br />
Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi at nihil sit fugit adipisci aperiam alias enim sed voluptates quo! Quam inventore officia commodi laudantium amet in sunt est quasi. <br />
Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi at nihil sit fugit adipisci aperiam alias enim sed voluptates quo! Quam inventore officia commodi laudantium amet in sunt est quasi. <br />
{ ... }
</div>
</div>
ScrollActive ๋ผ๋ state๊ฐ true ์ผ๋๋ fixedBox ์ fixed ํด๋์ค๋ฅผ ์ถ๊ฐํ๊ณ 'I am fixed! ๐' ๋ผ๋ ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅ ํ๋๋ก ํ๊ณ , flase ์ผ๋๋ fixedBox ํด๋์ค์ 'I will be fixed! ๐' ๋ผ๋ ๋ฌธ๊ตฌ๋ฅผ ์ถ๋ ฅ ํฉ๋๋ค.
top ์ ๋ถ๊ณ width๊ฐ ๋ณ๊ฒฝ๋๋ ๋ถ๋ถ์ ๋ชจ๋ .fixedBox.fixed ํด๋์ค๋ช ์ ์ฐธ์กฐํ css์์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
๊ทธ๋ฌ๋ฉด ํต์ฌ์ด์ ๋จ์ ๋ถ๋ถ์ window ์คํฌ๋กค์ ๊ฐ์ ํ์ธํ์ฌ 300 ์ด์์ธ ๊ฒฝ์ฐ ScrollActive ์ ๊ฐ์ true ๋ก ์ดํ์ธ ๊ฒฝ์ฐ๋ ๋ค์ false๋ก ๋ณ๊ฒฝํด ์ฃผ๋ ์ผ๋ง ๋จ์์ต๋๋ค.
- scroll ๊ฐ์ ๊ฐ์ ธ์ค๊ธฐ : window.pageYOffset
- scroll ํ์ ์ฌ๋ถ๋ฅผ ํ๋จํ๊ธฐ : addEventListener
const [ScrollY, setScrollY] = useState(0); // window ์ pageYOffset๊ฐ์ ์ ์ฅ
const [ScrollActive, setScrollActive] = useState(false);
function handleScroll() {
if(ScrollY > 299) {
setScrollY(window.pageYOffset);
setScrollActive(true);
} else {
setScrollY(window.pageYOffset);
setScrollActive(false);
}
}
useEffect(() => {
function scrollListener() { window.addEventListener("scroll", handleScroll); } // window ์์ ์คํฌ๋กค์ ๊ฐ์ ์์
scrollListener(); // window ์์ ์คํฌ๋กค์ ๊ฐ์
return () => { window.removeEventListener("scroll", handleScroll); }; // window ์์ ์คํฌ๋กค์ ๊ฐ์๋ฅผ ์ข
๋ฃ
});
useEffect ๋ก ์ค์๊ฐ ๊ฐ์๋ฅผ ํด์ค์ผํ๋๋ฐ, ์ฃผ์ํ ์ ์ ๋ฐ๋์ ์ด๋ฒคํธ๋ฆฌ์ค๋๋ฅผ ์ญ์ ํด ์ค์ผ ์คํฌ๋กคํ ๋ 2-3๋ฒ์ฉ ๋ ๋ ๋์ง ์์ต๋๋ค.
์ ์ฒด ์์ค
'๊ฐ๋ฐ ๐พ > ReactJS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React ์์ select ์ฌ์ฉํ๊ธฐ (6) | 2021.06.14 |
---|---|
React div ์คํฌ๋กค์ Fix ํ๊ธฐ (Element scroll) (0) | 2021.06.11 |
React์์ ๊ฐ๋ก ์คํฌ๋กค ๊ตฌํํ๊ธฐ (react-horizontal-scrolling-menu) (2) | 2021.06.03 |
[React][ํ๊ฒฝ์ธํ ] react-router-dom ์ฌ์ฉํ๊ธฐ (0) | 2021.05.25 |
[์๋ฌ๋ ธํธ] defaultState for reducer handling [object Object] should be defined (0) | 2021.03.17 |