javascript - 如何检测元素可见性(z-index、固定、不透明度等)(Intersection Observer V2 替代方案)
问题描述
Intersection Observer V2 将引入新功能,以根据不透明度、z-index 和固定定位等因素检测实际可见度。
信息:https ://developers.google.com/web/updates/2019/02/intersectionobserver-v2
问题:是否有替代方法或 polyfill 来检测当前浏览器中的实际可见性?
测试:https ://jsfiddle.net/v3kgewhf/
// Intersection Observer V2
const observer = new IntersectionObserver((changes) => {
for (const change of changes) {
// ⚠️ Feature detection
if (typeof change.isVisible === 'undefined') {
// The browser doesn't support Intersection Observer v2, falling back to v1 behavior.
change.isVisible = true;
}
if (change.isIntersecting && change.isVisible) {
visibleSince = change.time;
} else {
visibleSince = 0;
}
}
}, {
threshold: [1.0],
// Track the actual visibility of the element
trackVisibility: true,
// Set a minimum delay between notifications
delay: 100
}));```
解决方案
另一种方法是轮询您希望使用 setInterval 延迟方法检测可见性的点,该DocumentOrShadowRoot.elementFromPoint
延迟类似于您将用作 Intersection Observer v2 的延迟。
这是此时两者之间的 caniuse 链接,其中elementFromPoint支持 99%+ 的用户,而Intersection Observer v2仅支持 69%+。
推荐阅读
- .net-core - 如何在不执行请求的情况下获取 polly 断路器状态
- firebase - 当前时间戳和数据库时间戳错误之间的 Firebase 时间戳差异
- python - Pandas 过滤数据框以仅显示包含零的行
- python - 基于熊猫中年份列的累积计数
- python - Windows 和 LInux 上的 USB 设备/PyUSB 行为不同
- android - 实时存储问题经纬度 Firebase 并具有以下代码
- c# - 将字节数组发送到套接字服务器
- mysql - 自定义过滤器并将过滤后的数据导出到 Excel 工作表中,在 codeigniter 中不起作用
- html - 无法在 Django 中设置背景图像
- python - 按数组范围进行 Numpy 索引