我创建了一个在iframe内部显示three.js模型的网站。我试图将用户重定向回主页(index.html),只要他们不活动x分钟。我已经使用了一些java脚本,但问题是,只有在iframe之外活动时,它才能工作。当我实际旋转模型并与它交互时,它仍然重定向我。我一直在研究这个问题还没找到解决办法。我尝试调用iframe中的函数
onload="setup();"但这不起作用,还有很多其他的。这是我的密码
Javscript
var timeoutID;
function setup() {
this.addEventListener("mousemove", resetTimer, false);
this.addEventListener("mousedown", resetTimer, false);
this.addEventListener("mouseover", resetTimer, false);
this.addEventListener("mouseout", resetTimer, false);
this.addEventListener("keypress", resetTimer, false);
this.addEventListener("DOMMouseScroll", resetTimer, false);
this.addEventListener("mousewheel", resetTimer, false);
this.addEventListener("touchmove", resetTimer, false);
this.addEventListener("MSPointerMove", resetTimer, false);
startTimer();
}
function startTimer() {
// wait 5 seconds before calling goInactive
timeoutID = window.setTimeout(goInactive, 5000);
}
function resetTimer(e) {
window.clearTimeout(timeoutID);
goActive();
}
function goInactive() {
// do something
window.location = "index.html";
}
function goActive() {
// do something
startTimer();
}HTML
<main role="main" class="col-md-9 ml-sm-auto col-lg-12 px-0">
<!-- Featured Content -->
<div class="embed-responsive embed-responsive-16by9">
<div id="test">
<iframe style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none;
margin:0; padding:0; overflow:hidden; z-index:0;" class="embed-responsive-item" src="models/model.html" onload="setup();" allowfullscreen></iframe>
</div>
</div>
</main>发布于 2018-05-27 02:01:02
事件不会通过“障碍”( iframe )而浮出水面。我不太确定用例是什么,但iframe似乎并不是实现这一目标的最佳方法。
但是,如果这是必要的,那么我建议侦听iframe内容加载了iframe.onload事件(或者为load添加了一个事件侦听器),然后在iframe.contentWindow上添加必要的单击事件(只有在脚本来自同一来源时才能访问这些事件)。
const frame = document.createElement('iframe');
document.body.appendChild(frame);
frame.addEventListener('load', function() {
frame.contentWindow.addEventListener("mousemove", resetTimer, false);
frame.contentWindow.addEventListener("mousedown", resetTimer, false);
//..other events...
});
// ...
frame.src = ".../file.html";https://stackoverflow.com/questions/50548247
复制相似问题