ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๊ตฌํ˜„ ๊ฒฐ๊ณผ

๊ตฌํ˜„ ์˜์ƒ
๋‹ค์Œ๊ณผ ๊ฐ™์ด Scroll๊ฐ’์ด ํ™”๋ฉด ์ƒ์— ๋ ์ง€์ ์— ๋„๋‹ฌํ–ˆ์„ ๊ฒฝ์šฐ์— ์ถ”๊ฐ€์ ์œผ๋กœ ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜์—ฌ ํ™”๋ฉด์— ๋žœ๋”๋ง๋˜๋„๋ก ๊ธฐ๋Šฅ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

 

๋ฆฌ์•กํŠธ ์ƒ์—์„œ ์ „์—ญ ๊ฐ์ฒด(Window)์— ์Šคํฌ๋กค ์ด๋ฒคํŠธ๋ฅผ ๋ฐ”์ธ๋”ฉํ•ด์ฃผ๋Š” ์ž‘์—…์€ ์ด๋ฒˆ์— ์ฒ˜์Œํ•˜๋Š” ๊ฑฐ์˜€๊ธฐ ๋•Œ๋ฌธ์— ๊ด€๋ จ๋œ ์ฝ”๋“œ๋ฅผ ํฌ์ŠคํŒ…ํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.

 

์†Œ์Šค ์ฝ”๋“œ

์šฐ์„ , ์ „์—ญ ๊ฐ์ฒด Window์— Scroll Event๋ฅผ ๋ฐ”์ธ๋”ฉํ•ด์ฃผ๋Š” ์‹œ์ ์€ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ™”๋ฉด์— ๋‚˜์˜ค๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ ์ด๋‹ค.

 

Vanilla Javascript ์ƒ์—์„œ๋Š” ์ด ์ž‘์—…์„ DomContentLoaded ๋‚˜ load ๋œ ์‹œ์ ์— ํ•ด์ฃผ์ง€๋งŒ ๋ฆฌ์•กํŠธ ์ƒ์—์„œ๋Š” ํ•ด๋‹น ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ์— Scroll ์ด๋ฒคํŠธ๋ฅผ ๋ฐ”์ธ๋”ฉํ•ด์ฃผ๋Š” ๊ฒƒ๋ณด๋‹ค ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ฒ˜์Œ Mount(๋งˆ์šดํŠธ) ๋˜๋Š” ์‹œ์ ์— ํ•˜๋Š” ๊ฒƒ์ด ๋” ๋ช…ํ™•ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

 

์ด์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ฒ˜์Œ ๋งˆ์šดํŠธ ๋ฌ์„ ๋•Œ๋งŒ ์‹คํ–‰๋˜๋Š” componentDidMount ๋ผ์ดํ”„ ์‚ฌ์ดํด ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ Window ์ „์—ญ ๊ฐ์ฒด์— Scroll Event๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•ด์ฃผ์—ˆ๋‹ค.

componentDidMount() {
    window.addEventListener('scroll', this.checkFetchAddData, true);
    
    ( ... )
  }

 

๊ทธ ๋‹ค์Œ์œผ๋ก  ์Šคํฌ๋กค ์ด๋ฒคํŠธ๊ฐ€ ๋งค๋ฒˆ ๋ฐœ์ƒํ•  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋˜๋Š” checkFetchAddData ๋‚ด์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ˜„์žฌ ์Šคํฌ๋กค ๋œ ์œ„์น˜๊ฐ’์„ ๊ณ„์† ๊ณ„์‚ฐํ•ด์ฃผ์—ˆ๊ณ  Entry ์ง€์ ์— ๋„๋‹ฌํ•˜๊ฒŒ ๋˜๋Š” ์ˆœ๊ฐ„ ์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๋Š” ์—ฐ์‚ฐ์„ ํ•˜์—ฌ ์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์™€ ํ™”๋ฉด์— ๋žœ๋”๋ง๋˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

checkFetchAddData = event => {
    let { scrollTop, scrollHeight, clientHeight } =
      event.target.scrollingElement;

    let targetScrollPos = scrollHeight - parseInt(scrollTop); // ์Šคํฌ๋กค ๋ ์ง€์  ๊ณ„์‚ฐ

    if (targetScrollPos === clientHeight) // ์Šคํฌ๋กค์ด ๋ ์ง€์ ์— ๋„๋‹ฌํ–ˆ์„ ๊ฒฝ์šฐ
      this.sequenceFetchPath(this.fetchSoleFeedsAddPaths); // ์„œ๋ฒ„์— ์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ ์š”์ฒญ
  };

ํ•˜์ง€๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€์„ ๊ฒฝ์šฐ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ ๋‚ด์—์„œ ์Šคํฌ๋กค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์„ ๋•Œ, ํ•ด๋‹น ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ๊ณ„์† ์‹คํ–‰๋˜๋ฉด์„œ ์˜๋„์น˜ ์•Š๋Š” ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋‹ค.

 

์ •ํ™•ํ•œ ์›์ธ์€ ์ฐพ์ง€ ๋ชปํ•˜์˜€์ง€๋งŒ Window ์ „์—ญ ๊ฐ์ฒด์— Scroll Event๋ฅผ ๋ฐ”์ธ๋”ฉํ•ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๊ทธ ์•„๋ž˜์— ์ž์‹ ์š”์†Œ๋“ค ๋˜ํ•œ ๊ฐ„์„ญ์„ ๋ฐ›๋Š” ๊ฒƒ์ด๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๊ณ  ํ•ด๋‹น ๋ฉ”์„œ๋“œ ์ƒ๋‹จ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ์š”์†Œ๋“ค์ผ ๊ฒฝ์šฐ์—๋Š” return ์ข…๋ฅ˜ํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ๋ฆฌํŒฉํ† ๋งํ–ˆ๋‹ค.

checkFetchAddData = event => {
    if (
      event.target.className === 'feedsCarousel' ||
      event.target.className === 'commentContainer' ||
      event.target.className === 'profileContainer'
    )
      return;
    
    ( ... )
  };

์‚ฌ์‹ค ๊ฐ€์ƒ DOM์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฆฌ์•กํŠธ ๋‚ด์—์„œ ์ด๋ ‡๊ฒŒ DOM์— ์ง์ ‘ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ์‹์€ ๋ณธ์—ฐ์— ์ถฉ์‹ค์น˜ ๋ชปํ•˜๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ ๋‹ค.

ํ–ฅํ›„ ๋ฆฌ์•กํŠธ ์ƒ์—์„œ ์ด๋ฒคํŠธ ์ „ํŒŒ ๋ฐฉ์ง€๋ฅผ ์œ„ํ•œ ๋‹ค๋ฅธ ๋ฐฉ์•ˆ์ด ์žˆ๋Š”์ง€์— ๋Œ€ํ•ด์„œ ์ถ”๊ฐ€๋กœ ๊ณต๋ถ€๋ฅผ ํ•œ ๋’ค์— ํฌ์ŠคํŒ…ํ•˜๊ณ ์ž ํ•œ๋‹ค.

 

๊ด€๋ จ ํฌ์ŠคํŒ…

https://choi95.tistory.com/102

 

์Šคํฌ๋กค ์œ„์น˜ ๊ฐ€์ ธ์˜ค๊ธฐ

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹๋“ค์„ ํ†ตํ•ด ์Šคํฌ๋กค ์œ„์น˜์— ๋”ฐ๋ผ ๊ฐ๊ธฐ ๋‹ค๋ฅธ Header ๋””์ž์ธ์ด ๋žœ๋”๋ง๋˜๋„๋ก ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. scrollTop window.addEventListener('scroll', (event) => { let {scrollTop} = event.target.scroll..

choi95.tistory.com

 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2024/12   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ