๋ฅ์คํธ๋ก ๋ง๋ ํ๋ก์ ํธ์์ ์คํฌ๋กค๊ฐ์ ๋ฐ๋ผ์ ํด๋๊ฐ๋ค๋ฅด๊ฒ ๋ค๋ฅด๊ฒ ๊ตฌํํ๋๋ฐ ์คํฌ๋กค ์ด๋ฒคํธ์ ๋ํด์ ์ซ ๊ณต๋ถํด๋ณผ ํ์๊ฐ ์๋ ๊ฒ ๊ฐ์์ intersection Observer์ ๋ํด์ ๊ณต๋ถํด๋ณด๋ ค๊ณ ํ๋ค.
โ Intersection Observer๋
- Intersection observer๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ธ๋ผ์ฐ์ ๋ทฐํฌํธ(Viewport)์ ์ค์ ํ ์์(Element)์ ๊ต์ฐจ์ ์ ๊ด์ฐฐํ๋ฉฐ, ์์๊ฐ ๋ทฐํฌํธ์ ํฌํจ๋๋์ง ํฌํจ๋์ง ์๋์ง, ๋ ์ฝ๊ฒ๋ ์ฌ์ฉ์ ํ๋ฉด์ ์ง๊ธ ๋ณด์ด๋ ์์์ธ์ง ์๋์ง๋ฅผ ๊ตฌ๋ณํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
- ์ด ๊ธฐ๋ฅ์ ๋น๋๊ธฐ์ ์ผ๋ก ์คํ๋๋ฉฐ, scroll ๊ฐ์ ์ด๋ฒคํธ ๊ธฐ๋ฐ์ ์์ ๊ด์ฐฐ์์ ๋ฐ์ํ๋ ๋ ๋๋ง ์ฑ๋ฅ์ด๋ ์ด๋ฒคํธ ์ฐ์ ํธ์ถ ๊ฐ์ ๋ฌธ์ ์์ด ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ฌ์ฉ์ด์ ๋ฅผ ์ดํด๋ณด๊ธฐ ์ Intersection์ด๋ผ๋ ์๋ฏธ์ ๋ํด์ ์์๋ณผ ํ์๊ฐ ์๋ค. Intersection์ด๋ ๊ต์ฐจ์ ,๊ต์งํฉ์ด๋ผ๋ ์๋ฏธ๋ฅผ ๊ฐ๊ณ ์๋ค.
intersection observer๋ ๊ต์ฐจ์ ์ ๊ด์ธกํ๊ธฐ ์ํด์ ์ฌ์ฉํ๋ API๋ผ๋ ์๋ฏธ๋ฅผ ๊ฐ๊ณ ์๋๊ฒ์ ์ ์ ์๋ค.
์ฆ, Target Element ๊ฐ ํ๋ฉด์ ๋ ธ์ถ๋์๋ ์ง ์ฌ๋ถ๋ฅผ ๊ฐ๋จํ๊ฒ ๊ตฌ๋ ํ ์ ์๋ API ์ด๋ค.
์๋ฅผ ๋ค์ด, intersection observer๊ฐ ์์ด ๊ตฌํํ๋ค๊ณ ํ๋ค๋ฉด, scroll์ด ์ผ์ด๋ ๋ ๋ง๋ค, ํน์ element๊ฐ ํ๋ฉด์ ์กด์ฌํ๋์ง์ ๋ํ ์ฌ๋ถ๋ฅผ ๊ณ์ ๊ณ์ฐํ๋ code๋ฅผ ๋ง๋ค์ด์ผ ํ๋ค. ํ์ง๋ง intersection์ ์ฌ์ฉํ๋ค๋ฉด ์ฝ๊ฒ ์ฌ์ฉ์ฌ๋ถ๋ฅผ ํ์ธ ํ ์ ์๋ค.
1. ํ์ด์ง ์คํฌ๋กค ์ ์ด๋ฏธ์ง๋ฅผ Lazy Loadingํ ๋
2. Infinite scrolling์ ํตํด ์คํฌ๋กค์ ํ๋ฉฐ ์๋ก์ด ์ฝํ ์ธ ๋ฅผ ๋ถ๋ฌ์ฌ ๋
3. ๊ด๊ณ ์ ์์ต์ ๊ณ์ฐํ๊ธฐ ์ํด ๊ด๊ณ ์ ๊ฐ์์ฑ์ ์ฐธ๊ณ ํ ๋
4. ์ฌ์ฉ์๊ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ๊ฒ์ธ์ง์ ๋ฐ๋ผ ์ ๋๋ฉ์ด์ ๋์ ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ ๋
intersection observer๋ฅผ ์์ฑํ๊ธฐ ์ํด์๋ ์์ฑ์ ํธ์ถ ์ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ ๊ณตํด์ผ ํ๋ค. ์ด ์ฝ๋ฐฑ ํจ์๋ threshold๊ฐ ํ ๋ฐฉํฅ ํน์ ๋ค๋ฅธ ๋ฐฉํฅ์ผ๋ก ๊ต์ฐจํ ๋ ์คํ๋๋ค.
const observer= new IntersectionObserver(callback,options)
const intersectionObserver = new IntersectionObserver((entries, observer) => { entries.forEach((entry) => { if (entry.isIntersecting) { console.log(entry); observer.unobserve(entry.target); } }); }); intersectionObserver.observe(document.getElementById("app"));
entries๋ IntersectionObserverEntry ์ธ์คํด์ค์ ๋ฐฐ์ด์ด๋ค.
IntersectionObserverEntry๋ ์ฝ๊ธฐ ์ ์ฉ(Read only)์ ๋ค์ ์์ฑ๋ค์ ํฌํจํ๋ค.
boundingClientRect(DOMRectReadOnly)
: ๊ด์ฐฐ ๋์์ ์ฌ๊ฐํ ์ ๋ณดintersectionRect(DOMRectReadOnly)
: ๊ด์ฐฐ ๋์์ ๊ต์ฐจํ ์์ญ ์ ๋ณดintersectionRatio
: ๊ด์ฐฐ ๋์์ ๊ต์ฐจํ ์์ญ ๋ฐฑ๋ถ์จ(intersectionRect ์์ญ์์ boundingClientRect ์์ญ๊น์ง ๋น์จ, Number)isIntersecting(Boolean)
: ๊ด์ฐฐ ๋์์ ๊ต์ฐจ ์ํrootBounds(DOMRectReadOnly)
: ์ง์ ํ ๋ฃจํธ ์์์ ์ฌ๊ฐํ ์ ๋ณดtarget(Element)
: ๊ด์ฐฐ ๋์ ์์
let options={ root: document.querySelector('#target'), rootMargin:'0px', threshold:1.0 } let observer = new IntersectionObserver(callback, options);
const observer= new InterSectionObserver((entries) => { entries.forEach(entry => console.log(entry)) },{threshold:0.3}); const target= document.querySelector('#element'); oberver.observe(target) // ๊ด์ฐฐ์์ observer.unobserver(target) // ๊ด์ฐฐ ์ค์ง observer.disconnect() // IntersectionObserver ์ธ์คํด์ค๊ฐ ๊ด์ฐฐํ๋ ๋ชจ๋ ์์ ๊ด์ฐฐ ์ค์ง
const observer= new InterSectionObserver((entries,observer) => { entries.forEach(entry => { if(!entry.isIntersection){ return false; //๊ต์ฐจ ์ํ๊ฐ ์๋ ๋ return } //๊ด์ฐฐ๋์์ด ๊ต์ฐจ์ํ์ผ๋๋ง ์คํ console.log('๊ต์ฐจ์ํ ํ์ธ:',entry.isIntersection); //์ ์คํ์ ์ฒ๋ฆฌํ๊ณ ๊ด์ฐฐ ์ค์ง. observer.unobserve(entry.target); }) },{threshold:0.3});
์ถ์ฒ