首页 > 其他分享 >检测元素是否在屏幕中

检测元素是否在屏幕中

时间:2023-01-29 09:12:44浏览次数:32  
标签:document const observer 检测 元素 observe entry 屏幕 btn

 

const callback = (entries) => {
  entries.forEach((entry) => {
    if (entry.isIntersecting) {
      // `entry.target` is the dom element
      console.log(`${entry.target.id} is visible`);
    }
  });
};

const options = {
  threshold: 1.0,
};
const observer = new IntersectionObserver(callback, options);
const btn = document.getElementById("btn");
const bottomBtn = document.getElementById("bottom-btn");
observer.observe(btn);
observer.observe(bottomBtn);

 

标签:document,const,observer,检测,元素,observe,entry,屏幕,btn
From: https://www.cnblogs.com/webljl/p/17071693.html

相关文章