有没有办法从跨域 iframe 内部确定 iframe 是否在视图中?我试图使用 Intersection Observer API 来实现这一点。但它似乎仅适用于同域 iframe 而不适用于跨域。我检查了 Intersection Observer API 文档(在MDN和W3C 上),但找不到与此相关的任何内容。我希望我在这里没有遗漏任何东西。
这是示例代码
主 HTML 页面
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Main Page</title>
</head>
<body>
<div style="margin:700px auto;text-align:center;">
<iframe marginwidth="0" marginheight="0" frameborder="0" height="250px" width="300px"
id="aax_if_aax_sidebar-btf-1" allowtransparency="true" src="http://127.0.0.1:8080/iframe.html"></iframe>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
嵌入式 iframe 页面
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Iframe</title>
</head>
<body>
<div id="abc" style="background-color: black;width:100%;height: 100%;"></div>
<script>
setupIntersectionObserver = function (adContainer) {
console.log('setting up observer', observer);
var observer = new IntersectionObserver(
function …Run Code Online (Sandbox Code Playgroud)