移开视线时刷新 iframe(从 iframe 本身)
Refresh iframe when looking away (from within iframe itself)
我有一个页面嵌入了带有 Unity 游戏的 iframe,直到用户实际点击 iframe 时游戏才会加载。这是为了确保页面加载速度快,并且没有 运行 未玩的游戏(一个页面上可以有多个游戏)。
现在我想知道,是否可以在用户滚动离开时让 iframe 刷新?所以游戏停止 运行 并再次等待点击。是否可以仅更改 iframe 显示的页面? (所以不是包含 iframe 的页面。)
注意:我对嵌入 iframe 的实际页面的控制有限,这就是为什么如果这可以通过 iframe 显示的页面来完成会很好。
编辑:我发现这个是为了刷新:self.location.reload();
,现在我只需要能够检测 iframe 是否在 iframe 中可见。
您无法检测 iframe 是否在外部站点的视口中,因为您无法从 iframe 中访问父域的文档。这将需要确定 iframe 元素在页面上的位置。
如果 iframe src 与父 window 在同一个域中,那么这是可能的。在您的游戏页面中,您可以检测到父级 window 中的滚动事件,如下所示:
var parent = window.parent;
parent.onscroll = function (e) {
// the parent window has scrolled,
// you can pause the game here
}
然后您可以通过添加点击事件来检测用户是否点击了 iframe。
window.onclick = function() {
// user has clicked on the iframe,
// you can initialise the game now
}
我有一个页面嵌入了带有 Unity 游戏的 iframe,直到用户实际点击 iframe 时游戏才会加载。这是为了确保页面加载速度快,并且没有 运行 未玩的游戏(一个页面上可以有多个游戏)。
现在我想知道,是否可以在用户滚动离开时让 iframe 刷新?所以游戏停止 运行 并再次等待点击。是否可以仅更改 iframe 显示的页面? (所以不是包含 iframe 的页面。)
注意:我对嵌入 iframe 的实际页面的控制有限,这就是为什么如果这可以通过 iframe 显示的页面来完成会很好。
编辑:我发现这个是为了刷新:self.location.reload();
,现在我只需要能够检测 iframe 是否在 iframe 中可见。
您无法检测 iframe 是否在外部站点的视口中,因为您无法从 iframe 中访问父域的文档。这将需要确定 iframe 元素在页面上的位置。
如果 iframe src 与父 window 在同一个域中,那么这是可能的。在您的游戏页面中,您可以检测到父级 window 中的滚动事件,如下所示:
var parent = window.parent;
parent.onscroll = function (e) {
// the parent window has scrolled,
// you can pause the game here
}
然后您可以通过添加点击事件来检测用户是否点击了 iframe。
window.onclick = function() {
// user has clicked on the iframe,
// you can initialise the game now
}