元问答栏目视频美女
  1. 编程问答
  2. 答案列表
  3. 答案正文

JavaScript 检测元素是否在屏幕中

检查元素是否在窗口中最好的方法是使用intersectionobserver
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);
@sqlkk评:0
猜你喜欢