[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/API/Intersection_Observer_API [Back]  [Original]

Intersection Observer API - Web API | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Intersection Observer API

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2019 3.

* Some parts of this feature may have varying levels of support.

Intersection Observer API viewport .

, . Web , . .

  • (Lazy-loading) .
  • " " , .
  • .
  • .

Element.getBoundingClientRect() . , . , .

. , , . . . , , , .

Intersection Observer API ( viewport) . , , .

Intersection Observer API : . API " N% " .

In this article

Intersection Observer API .

  • . Intersection Observer API .
  • observer .

, , . root null . .

, API .

intersection ratio . 0.0 1.0 .

intersection observer , threshold callback .

js
let options = {
  root: document.querySelector("#scrollArea"),
  rootMargin: "0px",
  threshold: 1.0,
};

let observer = new IntersectionObserver(callback, options);

threshold 1.0 root 100% . .

IntersectionObserver() options observer . .

root

. . null .

rootMargin

. CSS margin . . "10px 20px 30px 40px" (, , , ). . . 0.

threshold

. 50% , 0.5 . 25% , [0, 0.25, 0.5, 0.75, 1] . 0 . (1 , .) 1.0 .

, .

js
let target = document.querySelector("#listItem");
observer.observe(target);

// observer       
//     . (   )

IntersectionObserver , . IntersectionObserverEntry .

js
let callback = (entries, observer) => {
  entries.forEach((entry) => {
    //       .
    //  :
    //   entry.boundingClientRect
    //   entry.intersectionRatio
    //   entry.intersectionRect
    //   entry.isIntersecting
    //   entry.rootBounds
    //   entry.target
    //   entry.time
  });
};

. isIntersecting .

. . , Window.requestIdleCallback() .

, root , .

Intersection Observer API . . , , .

IntersectionObserverEntry .

, . . document null .

. .

IntersectionObserver , root margin , rootMargin . rootMargin ( IntersectionObserverEntry.rootBounds ) .

Thresholds

Intersection Observer API thresholds . , . , API .

, 25% , [0, 0.25, 0.5, 0.75, 1] .

, IntersectionObserverEntry .

isIntersecting . true , . .

boundingClientRect 0 . .

, . , . .

. .

  1. (, ) getBoundingClientRect() . . .
  2. , . overflow . overflow visible clipping .
  3. (<iframe> ) , , . <iframe> , , .
  4. , .
  5. root intersection rectangle .
  6. document .

, IntersectionObserver . , IntersectionObserverEntry IntersectionObserver .

IntersectionObserverEntry , , , .

75% . 0.0 () approximately isIntersecting . , intersectionRatio 75% .

js
const intersectionCallback = (entries) => {
  entries.forEach((entry) => {
    if (entry.isIntersecting) {
      let elem = entry.target;

      if (entry.intersectionRatio >= 0.75) {
        intersectionCounter++;
      }
    }
  });
};

IntersectionObserver

Intersection Observer API . . Document viewport . root .

IntersectionObserverEntry

. IntersectionObserver IntersectionObserver.takeRecords() .

. Intersection Observer API , (: ) , .

HTML

HTML ( ID "box") .

html
<div id="box">
  <div class="vertical">Welcome to <strong>The Box!</strong></div>
</div>

CSS

CSS . background-color border CSS transitions , .

css
#box {
  background-color: rgba(40, 40, 190, 1);
  border: 4px solid rgb(20, 20, 120);
  transition:
    background-color 1s,
    border 1s;
  width: 350px;
  height: 350px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.vertical {
  color: white;
  font: 32px "Arial";
}

.extra {
  width: 350px;
  height: 350px;
  margin-top: 10px;
  border: 4px solid rgb(20, 20, 120);
  text-align: center;
  padding: 20px;
}

, Intersection Observer API JavaScript .

, observer .

js
const numSteps = 20.0;

let boxElement;
let prevRatio = 0.0;
let increasingColor = "rgba(40, 40, 190, ratio)";
let decreasingColor = "rgba(190, 40, 40, ratio)";

// Set things up
window.addEventListener(
  "load",
  (event) => {
    boxElement = document.querySelector("#box");

    createObserver();
  },
  false,
);

.

numSteps

0.0 1.0 .

prevRatio

. .

increasingColor

. "ratio" , .

decreasingColor

, .

load Window.addEventListener() . , querySelector() "box" ID , createObserver() .

createObserver() IntersectionObserver .

js
function createObserver() {
  let observer;

  let options = {
    root: null,
    rootMargin: "0px",
    threshold: buildThresholdList(),
  };

  observer = new IntersectionObserver(handleIntersect, options);
  observer.observe(boxElement);
}

options . root null . rootMargin "0px" . ( ) .

threshold buildThresholdList() . .

options , IntersectionObserver() , handleIntersect() . observe() .

observer.observe() .

buildThresholdList() .

js
function buildThresholdList() {
  let thresholds = [];
  let numSteps = 20;

  for (let i = 1.0; i <= numSteps; i++) {
    let ratio = i / numSteps;
    thresholds.push(ratio);
  }

  thresholds.push(0);
  return thresholds;
}

1 numSteps i i/numSteps thresholds 0.0 1.0 . 0 . , numSteps(20) .

# Ratio # Ratio
0 0.05 11 0.6
1 0.1 12 0.65
2 0.15 13 0.7
3 0.2 14 0.75
4 0.25 15 0.8
5 0.3 16 0.85
6 0.35 17 0.9
7 0.4 18 0.95
8 0.45 19 1
9 0.5 20 0
10 0.55

, , . .

( ID "box" ) , handleIntersect() .

js
function handleIntersect(entries, observer) {
  entries.forEach((entry) => {
    if (entry.intersectionRatio > prevRatio) {
      entry.target.style.backgroundColor = increasingColor.replace(
        "ratio",
        entry.intersectionRatio,
      );
    } else {
      entry.target.style.backgroundColor = decreasingColor.replace(
        "ratio",
        entry.intersectionRatio,
      );
    }

    prevRatio = entry.intersectionRatio;
  });
}

entries IntersectionObserverEntry intersectionRatio . , background-color increasingColor(. "rgba(40, 40, 190, ratio)" .) , "ratio" intersectionRatio . , . , .

, intersectionRatio , decreasingColor background-color "ratio" intersectionRatio .

, prevRatio .

. .

. Timing element visibility with the Intersection Observer API.

Specification
Intersection Observer
# intersection-observer-interface


Web Proxy Viewer  |  New URL  |  Original Page