Infinite scroll with intersectionObserver API - connect-foundation/2019-12 GitHub Wiki
์คํฌ๋กค์ด ํฌํจ๋ ๋ฆฌ์คํธ ์ปดํฌ๋ํธ๋ฅผ ๊ฐ๋ฐํ๋ค๊ณ ์๊ฐํด๋ด ์๋ค. ์๋์ ๊ฐ์ด 3๊ฐ์ ListItem์ด ๋ด๊ธด ๋ฆฌ์คํธ๋ ๋ ๋๋งํ๋ ๋ฐ ์ ํ ๋ฌธ์ ๊ฐ ์์ ๊ฒ์ ๋๋ค. (ํ ๋์ ๋ค์ด์ฌ ๊ฒ์ ๋๋ค! ๐)
<List>
<ListItem>1</ListItem>
<ListItem>2</ListItem>
<ListItem>3</ListItem>
</List>๊ทธ๋ฐ๋ฐ ๋ง์ฝ ListItem์ด 1000๊ฐ ๋ผ๊ณ ๊ฐ์ ํด๋ด ์๋ค. ์ผ๋ฐ์ ์ธ ์ํฉ์ด๋ผ๋ฉด ํ๋์ ๋ค ๋ค์ด์ค์ง ์์ ๊ฒ์ ๋๋ค. ๊ทธ๋ ๋ค๋ฉด ์นํ์ด์ง๋ฅผ ๋ก๋ฉํ ๋, ํ๋ฒ์ 1000๊ฐ๋ฅผ ๋ค ๋ก๋ฉ์ํค๋ ๊ฑด ๊ณผ์ฐ ์ข์ ๋ฐฉ๋ฒ์ผ๊น์? ๋ค์ํ ์๊ฒฌ์ด ์์ ์๋ ์๊ฒ ์ง๋ง, No ๋ผ๊ณ ์๊ฐํฉ๋๋ค.
์๋ํ๋ฉด ์์ ์์ ์์๋ ๋จ์ํ ํ ์คํธ์ง๋ง, ๋ณดํต์ ํ๊ฒฝ์์ ์ด๋ฏธ์ง๋ ๋ค์ด๊ฐ๊ฑฐ๊ณ ์คํ์ผ๋ง ๋ํ ๋ง์ ๊ฒ์ ๋๋ค. ์ด๊ฒ๋ค์ ๋ค ๋ก๋ฉ์ํค๋ ๊ฑด ๊ต์ฅํ ์์๋ญ๋น๋ผ๊ณ ์๊ฐํฉ๋๋ค. ๋ํ ํน์ํ ๊ฒฝ์ฐ๋ฅผ ์ ์ธํ๊ณค ์คํฌ๋กค์ ๋๊น์ง ๋ด๋ ค์ ๋ค ๋ณด๋ ์ ์ ๋ ๋ง์ด ์์ ๊ฒ์ ๋๋ค.
๋ฐ๋ผ์ List ์ปดํฌ๋ํธ๋ฅผ ๊ตฌํํ ๋, ๋ฆฌ์คํธ ์์ดํ ์ ๋์ ์ผ๋ก ๊ฐ์ ธ์ค๋ ๊ฒ์ ํ์์ ๋๋ค. ๊ทธ๋ ๋ค๋ฉด ์ด๋ป๊ฒ ๋์ ์ผ๋ก ๊ฐ์ ธ์ฌ ์ ์์๊น์? ์ด๋ฏธ ๋ง์ ํ์ด์ง๋ฅผ ๊ฒฝํํ๋ฉด์ ๋ฆฌ์คํธ ์คํฌ๋กค์ ๋ด๋ฆฌ๋ค ๋ณด๋ฉด ๋งจ ๋์ ๋๋ฌํ ๋ ์ฏค ์๋ก์ด ์์ดํ ๋ค์ ๊ฐ์ ธ์ค๋ ๊ฒ์ ์ ์ ์์ ๊ฒ์ ๋๋ค.
๋ฆฌ์คํธ ์คํฌ๋กค์ด ๋งจ ์๋์ฏค ๋๋ฌํ ๊ฒ์ ์ด๋ป๊ฒ ์์๋ธ๊ฑธ๊น์? ์ด์ ์๋ ์ฃผ๋ก ํน์ ์ปจํ ์ด๋์ ์ด๋ฒคํธ๋ฅผ ๋ฌ์์ ์คํฌ๋กค์ ๊ณ์ํด์ ํ์งํ๋ฉฐ ์ผ์ ๋ถ๋ถ์ ๋๋ฌํ์ ๋๋ฅผ ๊ฐ์งํ์ต๋๋ค. ์ด๋ ๊ฒ ๋๋ฉด ์๋ฏธ์๋ ์คํฌ๋กค ์ด๋ฒคํธ๋ ๊ณ์ํด์ ์ฝ๊ธฐ ๋๋ฌธ์ ์์ ๋ญ๋น๊ฐ ๋ฐ์ํ๊ฒ ๋ฉ๋๋ค.
๊ทธ๋ ๋ค๋ฉด ์กฐ๊ธ์ ๋ ์ค๋งํธํ๊ฒ ํน์ ์์ญ์ ๋๋ฌํ ๊ฑธ ์ ์ ์์๊น์? ๋ฐ๋ก Web API ์ค ํ๋์ธ Intersection Observer API ์ด ์์ต๋๋ค.
The Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.
Target element์ ํน์ ์์ญ์ ๊ต์ฐจ์ ์์์ ๋ณํ๋ฅผ ๋น๋๊ธฐ์ ์ผ๋ก ๊ด์ฐฐํ๋ค. ์์ฃผ ์ฝ๊ฒ ๋ณด๋ฉด "Element๊ฐ ํน์ ์์ญ์์ ๋ณด์ด๋ ๊ฐ?"๋ฅผ ์ฝ๊ฒ ์๋ ค์ค๋ค๋ ๊ฒ์ ๋๋ค. ์๋์ ์ค๋ช ์์๋ viewport, ์ฆ ํ๋ฉด์ ๊ธฐ์ค์ผ๋ก ํน์ ์์ญ์ ์ค๋ช ํ๊ฒ ์ต๋๋ค.
๊ทธ๋ ๋ค๋ฉด ์ด๊ฒ์ ์ฌ์ฉํ์ฌ Infinite scroll ์ ์ด๋ป๊ฒ ๊ตฌํํ ์ง์ ๋ํด ๊ฐ๋จํ ์ฝ๋๋ก ์์๋ด ๋๋ค.
<List class="list">
<ListItem>1</ListItem>
<ListItem>2</ListItem>
<ListItem>3</ListItem>
</List>
<div class="observer"></div>์ด๋ฐ์์ผ๋ก observer๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง div element๊ฐ ํ๋ฉด์ ๋ณด์ด๊ฒ ๋๋ค๋ฉด ListItem์ ์ถ๊ฐํ๋ ์์ดํ ์ ์คํ์ํฌ ์ ์์ ๊ฒ์ ๋๋ค.
const addListItem = () => {
document.querySelector('.list').innerHTML += `
<ListItem>4</ListItem>
<ListItem>5</ListItem>
<ListItem>6</ListItem>
`;
}๊ทธ๋ฌ๋ฉด ์ด์ ์ด๋ป๊ฒ Intersection Obeserver API๋ฅผ ์ฌ์ฉํ๋ ์ง์ ๋ํด ์์๋ด ์๋ค.
const options = {
root: document.querySelector('#scrollArea'),
rootMargin: '0px',
threshold: 1.0
};
const observer = new IntersectionObserver(callback, options)์์ ์ฝ๋๋ฅผ ์ค๋ช ํ๊ธฐ ์ ์ IntersectionObserver์ ๋ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ์ธ options๋ถํฐ ์ค๋ช ํด๋ณผ๊น์? options์๋ ์ธ ๊ฐ์ง๊ฐ ๋ค์ด๊ฐ๋๋ค.
The element that is used as the viewport for checking visiblity of the target. Must be the ancestor of the target. Defaults to the browser viewport if not specified or if null.
target element์ ๊ต์ฐจ์ ์ ๊ฐ์งํ Element์ ์ค์ ํฉ๋๋ค. default value๋ก๋ null ๊ฐ์ธ๋ฐ, null์ root๊ฐ์ด ์ค์ ๋๋ค๋ฉด ๋ธ๋ผ์ฐ์ ์ viewport๋ก ์์ญ์ด ์ค์ ๋๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. ๋ฐ๋ผ์ ๋ณด์ด๋ ํ๋ฉด ์ ์ฒด ๋ด์์์ ๋ณด์ด๋ ๊ฐ? ๋ฅผ ๋ณด๋ ค๋ฉด null์ ์ฃผ์ด์ผํ๊ณ , ํน์ ์์ญ ๋ด์์์ element๊ฐ ๋ณด์ด๋ ๊ฐ๋ฅผ ๋ณด๋ ค๊ณ ํ๋ค๋ฉด ํน์ ์์ญ์ Element๋ฅผ ์ฃผ์ด์ผ ํฉ๋๋ค.
Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left). The values can be percentages. This set of values serves to grow or shrink each side of the root element's bounding box before computing intersections. Defaults to all zeros.
์ด๋ฆ ๊ทธ๋๋ก์ root์ margin๊ฐ์ ์ค์ ํฉ๋๋ค. ์ฐ๋ฆฌ๊ฐ ์ตํ ์๊ณ ์๋ css์ margin๊ณผ ๊ฐ์ ๊ฐ๋
์
๋๋ค.
์๋ฅผ ๋ค์ด rootMargin: "0px 0px 200px 0px" ๋ผ๊ณ ์ค์ ํ๋ค๋ฉด root์ ์์ญ์์์ ๊ต์ฐจ์ ์ ํ์งํ๋ ๊ฒ์ด ์๋๋ผ ๋ง์ง์ ์์ญ์ ์ ์ธํ ์์ ์์ญ์์์ ๊ต์ฐจ์ ์ ํ์งํ๊ฒ ๋ฉ๋๋ค.
๊ฐ๋จํ๊ฒ ์ฃผ์ํ ๊ฒ์ ๋ฌธ์์ด๋ก ์์ฑ์ ํด์ผํฉ๋๋ค.
Either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. If you only want to detect when visibility passes the 50% mark, you can use a value of 0.5. If you want the callback to run every time visibility passes another 25%, you would specify the array [0, 0.25, 0.5, 0.75, 1]. The default is 0 (meaning as soon as even one pixel is visible, the callback will be run). A value of 1.0 means that the threshold isn't considered passed until every pixel is visible.
threshold๋ target element์ root๊ฐ ์ผ๋ง๋ ๊ต์ฐจํ์ ๋(์ผ๋ง๋ ๋ณด์์ ๋), callback์ ์คํํ ์ง๋ฅผ ๊ฒฐ์ ํ๋ ๊ฐ์
๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก๋ 0๋ถํฐ 1๊น์ง์ float๊ฐ์ ํ ๋นํฉ๋๋ค. 0%๋ถํฐ 100%, ๋ณด์์ ๋๋ฅผ ์๋ฏธํฉ๋๋ค.
์ ๊ธฐํ๊ฒ๋ ๋ฐฐ์ด๋ ํ ๋น์ ํ ์ ์๋๋ฐ [0, 0.25, 0.5, 0.75, 1] ๋ผ๊ณ ํ ๋น์ ํ๋ค๋ฉด 25% ๊ต์ฐจํ ๋๋ง๋ค ๋ฌด์ธ๊ฐ์ ๋์์ ํ ์ ์์ต๋๋ค.
์ข ํฉ์ ์ผ๋ก ๋ค์ ์ค๋ช ํ์๋ฉด, root์ target element๊ฐ threhold๋งํผ ๊ต์ฐจํ๊ฒ ๋๋ค๋ฉด callback์ด ์คํ๋ฉ๋๋ค!
๊ทธ๋ ๋ค๋ฉด callback์ ์ด๋ป๊ฒ ์ค์ ํ ๊น์?
์ฝ๋ฐฑ ํจ์์ ํ๋ผ๋ฏธํฐ๋ก๋ entries์ observer๊ฐ ์์ต๋๋ค.
entries: Element[] target์ผ๋ก ์ง์ ํ DOM elemet .
observer: IntersectionObserver ์๊ธฐ ์์ ์ธ observer
const callback = (entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
addListItem();
}
});
};์ด๋ฐ์์ผ๋ก isIntersecting ์ด๋ผ๋ ๋ฉ์๋๋ฅผ ํตํด์ ๊ฐ ์ํธ๋ฆฌ ์๋ฆฌ๋จผํธ๋ค์ด ๊ต์ฐจ์ ์ ๋๋ฌํ๋์ ๋ฐ๋ผ์ ๊ด๋ จ ํจ์๋ค์ ์คํ์ํฌ ์ ์์ต๋๋ค.