簡體   English   中英

Javascript:將Div置於視口中時對其進行動畫處理

[英]Javascript: Animate Div when it is in viewport

在圖片中,標題下方有4個圓圈,

當它們在視口中時,我無法弄清楚如何使其如圖所示進行動畫處理

在此處輸入圖片說明

在此處輸入圖片說明

您可以使用Intersection_Observer_API來檢查元素何時在視口中並應用動畫。

https://developer.mozilla.org/zh-CN/docs/Web/API/Intersection_Observer_API

function handleIntersectionEntry(entry) {
if (entry.length > 0) {
  const item = entry[0];
  const ratio = Math.floor(item.intersectionRatio);
  if (ratio > 0) {
    // ITEM IN VIEWPORT
  } else if (ratio < 1) {
   // ITEM OUT OF VIEWPORT
  }
}


  const config = {
    root: null,
    threshold: [1.0],
    rootMargin: "0px"
  };

  this.Observer = new IntersectionObserver(
    handleIntersectionEntry,
    config
  );

  this.Observer.observe('YOUR DOM ELEMENT TO MONITOR');

支持所有瀏覽器的Polyfill: https : //github.com/w3c/IntersectionObserver/tree/master/polyfill

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM