Intersection Observer
๋์์ ๋ ์ด์ด ํ๋ ์ด์ด ์์ ์ ์งํํ๋ฉด์, ํ์ฌ ํน์ element๊ฐ ๋ณด์ด๋์ง ์ฌ๋ถ์ ๋ฐ๋ผ์ ๋ ์ด์ด ํ๋ ์ด์ด ํน์ ์๋จ ๋น๋์ค ํ๋ ์ด์ด๋ฅผ ๋ ธ์ถ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํด์ผํ๋ ๊ฒฝ์ฐ๊ฐ ์๊ฒผ๋ค.

Intersection Observer API๋ element๊ฐ viewport์์ "๋ณด์ด๋์ง ์๋ณด์ด๋์ง"(visible)์ ๋ฐ๋ผ ๋น๋๊ธฐ๋ก ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ฅผ ํ ์ ์๊ฒ ํด์ค๋ค.
const intersectionObserver = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
// do something
observer.unobserve(entry.target);
}
});
});
intersectionObserver.observe(element);
๊ธฐ๋ณธ์ ์ธ ์ฌ์ฉ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
๊ทธ๋ฆฌ๊ณ ์๋์ 3๊ฐ์ง ์ต์ ์ ์ค ์ ์๋ค.
root :
Element
๋๋null
๊ฐ์ผ๋ก target (๊ด์ธก ๋์) ์ ๊ฐ์ธ๋ element ๋ฅผ ์ง์ ํ๋ค๊ณ ํ ์ ์์ต๋๋ค.๋ง์ฝ null ๋ก ์ง์ ํ๋ค๋ฉด viewport๋ก ์ง์ ๋ฉ๋๋ค.
rootMargin : root์์๋ฅผ ๊ฐ์ธ๋ margin ๊ฐ์ ์ง์ ํ๋ค.
threshold : target element ๊ฐ root ์ ๋ช % ๊ต์ฐจํ์ ๋, callback ์ ์คํํ ์ง ๊ฒฐ์ ํ๋ ๊ฐ์ด๋ค.
์ด๋ฒ์ ๊ฒฝ์ฐ์๋ ํค๋์ ๋์ด์ ํ์ฌ ํ๋ ์ด์ด๊ฐ ๋
ธ์ถ๋๋ ๋น์จ์ ๊ณ์ฐํ์ฌ ๋ ์ด์ด ํ๋ ์ด์ด๋ฅผ ๋
ธ์ถํด์ผํ๋ ๊ฒฝ์ฐ์ฌ์ threshold
์ต์
์ ์ฌ์ฉํด ์์ญ ๋
ธ์ถ์ฌ๋ถ๋ฅผ ํ๋จํ๋ค.
_setObserveIstance: function(videoPlayerView, controlOptions){
var s = this;
var $header = $('#header');
var $player = $('#player');
var threshold = ($header.outerHeight() / $player.height()).toPrecision(1);
var observerOptions = {
threshold: Number(threshold)
};
// ์ฌ์์ค์ธ ๊ฒฝ์ฐ ๋ ์ด์ด ํ๋ ์ด์ด ๋
ธ์ถ
var observerInstance = new IntersectionObserver(function(entries, observer) {
entries.forEach(function(entry){
if(entry.intersectionRatio > threshold){
// ๋ณด์ฌ์ง๋ ์์ญ ๋น์จ์ด threshold๋ณด๋ค ํฌ๋ฉด ์๋จ ํ๋ ์ด์ด ๋
ธ์ถ
}else{
// ๋ณด์ฌ์ง๋ ์์ญ ๋น์จ์ด threshold๋ณด๋ค ์์ผ๋ฉด ๋ ์ด์ดํ๋ ์ด์ด ๋
ธ์ถ
}
});
}, observerOptions);
observerInstance.observe(videoPlayerView.$el.get(0));
}
} );
๋์์์ด ์ผ์์ ์ง๋๊ฑฐ๋ ์ข
๋ฃ๋ ๊ฒฝ์ฐ์๋ observer.unobserve(element);
๋ก observing์ ์ข
๋ฃํด์ฃผ์๋ค.
์ฐธ์กฐ ํ์ด์ง
Last updated
Was this helpful?