检查元素是否在窗口中最好的方法是使用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);
- 编程问答
- 答案列表
JavaScript 检测元素是否在屏幕中[朗读]
加入收藏