javascript 元素滚动到视图时的回调

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 元素滚动到视图时的回调相关的知识,希望对你有一定的参考价值。

const observeScroll = (element, callback) => {
  const observer = new IntersectionObserver( ([entry]) => {
    if (entry.intersectionRatio < 1) return;

    callback(element);
    observer.disconnect(); // Stop watching the element
  }, 
  {
    threshold: 1
  });

  observer.observe(element); // Start watching the element
};

function startAnimation(element) { ... }

const element = document.getElementById("express-animation");
observeScroll(element, startAnimation);

以上是关于javascript 元素滚动到视图时的回调的主要内容,如果未能解决你的问题,请参考以下文章