檢查元素是否在窗口中最好的方法是使用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 檢測元素是否在螢幕中[朗讀]
加入收藏