๊ฐœ๋ฐœ ๐Ÿพ/ReactJS

React ์Šคํฌ๋กค์‹œ Fix ํ•˜๊ธฐ (Window scroll)

JOTOKKI 2021. 6. 8. 14:55
728x90

์Šคํฌ๋กค๋ฐ”๋ฅผ ์–ด๋Š ๊ฐ’๋งŒํผ ๋‚ด๋ฆฌ๋ฉด ํŠน์ • ์—˜๋ ˆ๋จผํŠธ์˜ 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๋ฒˆ์”ฉ ๋ Œ๋” ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 

 


์ „์ฒด ์†Œ์Šค 

 

 

๋ฐ˜์‘ํ˜•