有没有办法确定iframe是否可见/活动



我有一个在站点上运行的iframe(带有不同的域)。我可以在IFRAME中知道它是否对用户可行,或者此iFrame" live in in In in Inctive"是否处于活动状态。谢谢。

这实际上是不可能的,因为如果您在其他域/端口/方案上,则无法访问从框架本身嵌入帧的主页。p>如果您的iFrame在同一域中&&港口& amp;方案,您可以这样做:

<html>
<body>
    <iframe src="frame.htm" id="myframe" style="display:block"></iframe>
</body>
</html>

和frame.htm:

<script>
    var is_hidden = parent.document.getElementById("myframe").style.display == "none";
    alert(is_hidden ? "I am hidden" : "I am visible");
</script>

update 忽略了"来自其他域"的部分 - 问题的一部分,相应地更新了帖子。

您应该查看交叉点观察者API。我能够为两个不同的域名工作。

类似的东西对我有用:

function handleIntersect(entries, observer) {
  console.log('intersecting', entries[0].isIntersecting, 'visible', entries[0].isVisible);
}
let observer;
let options = {
  root: null,
  rootMargin: "0px",
  threshold: [0.5]
};
observer = new IntersectionObserver(handleIntersect, options);
// The DOM element here should be a div that captures the whole content of the iframe.
observer.observe(DOM_ELEMENT);

最新更新