我正在尝试制作类似于视频的网页。所以我需要暂停网页,当用户按下按钮并从该点继续。如何通过点击按钮暂停和恢复html页面的执行
回答
你不能“暂停”HTML的执行。您可以通过用户或使用计时器通过操作(按钮点击前的按钮)显示项目(最初使用css隐藏)来控制何时以及如何使用JavaScript的不同部分,以及使用javascript gets revealed。请参见暂停和播放的setTimeout和setInterval
function removeClass(el, className) {
if (el.classList)
el.classList.remove(className);
else {
var reg = new RegExp('(\\s|^)' + className + '(\\s|$)');
el.className = el.className.replace(reg, ' ');
}
}
function revealDivInTime(id, time) {
var div = document.getElementById(id);
if (div) {
setTimeout(function() {
removeClass(div, "hidden");
}, time);
}
}
window.onload = function() {
document.getElementById("start").onclick = function() {
revealDivInTime("one", 1000);
revealDivInTime("two", 5000);
revealDivInTime("three", 10000);
}
document.getElementById("end").onclick = function() {
revealDivInTime("four", 0);
revealDivInTime("five", 3000);
revealDivInTime("three", 10000);
}
}
.hidden {
display: none;
}
<div>
Click to start story: <button id="start">click me</button>
</div>
<div id="one" class="hidden">
<p>First Part of story revealed after 1 second</p>
</div>
<div id="two" class="hidden">
<p>Second Part of story revealed after 5 seconds</p>
</div>
<div id="three" class="hidden">
<p>Third Part of story revealed after 10 seconds</p>
<p> Click this button to finish Story: <button id="end">Finish</button>
</div>
<div id="four" class="hidden">
<p>This get's revealed only after user clicks on button in story part 3. Story will end in 3 seconds</p>
</div>
<div id="five" class="hidden">
<p>Story finished</p>
</div>
实例表明:
var currentScene = 0,
looper, sceneCount = 10,
isPlaying = false;
function removeClass(el, className) {
if (el.classList)
el.classList.remove(className);
else {
var reg = new RegExp('(\\s|^)' + className + '(\\s|$)');
el.className = el.className.replace(reg, ' ');
}
}
function nextScene() {
currentScene++;
if (currentScene > sceneCount) {
clearInterval(looper);
isPlaying = false;
return;
}
removeClass(document.getElementById("s"+currentScene),"hidden");
}
function play() {
if (isPlaying) {
return;
}
isPlaying = true;
looper = setInterval(nextScene, 2000);
}
function pause() {
if (!isPlaying) {
return;
}
isPlaying = false;
clearInterval(looper);
}
document.getElementById("play").onclick=function(){play();};
document.getElementById("pause").onclick=function(){pause();};
.hidden {
display: none;
}
<button id="play">Play</button><button id="pause">Pause</button>
<div id="s1" class="hidden">
Scene 1
</div>
<div id="s2" class="hidden">
Scene 2
</div>
<div id="s3" class="hidden">
Scene 3
</div>
<div id="s4" class="hidden">
Scene 4
</div>
<div id="s5" class="hidden">
Scene 5
</div>
<div id="s6" class="hidden">
Scene 6
</div>
<div id="s7" class="hidden">
Scene 7
</div>
<div id="s8" class="hidden">
Scene 8
</div>
<div id="s9" class="hidden">
Scene 9
</div>
<div id="s10" class="hidden">
Scene 10. Story Over. Click reload and click play to start again.
</div>
当我点击停止按钮时,如何暂停我的代码的一部分,并且只有当我点击播放按钮时才能继续。 –
您可以使用setInterval和removeInterval实现暂停/播放效果。保持变量跟踪当前场景,并使用setInterval(以及稍后将存储引用删除它)来定期显示场景(同时保持currentScene变量更新以跟踪用户最后看到的场景)。当用户按下暂停时,使用setInterval(这将“暂停”)时获得的存储引用上的removeInterval删除间隔。当用户恢复时,再次调用setInterval以基于追踪最后看到的场景的变量的状态,定期显示场景。 –
我无法理解这种方法。你能举一个例子吗? –
- 1. 如何在点击按钮时暂停和恢复uiview动画?
- 2. 暂停阅读excel和按钮点击恢复
- 3. 如何通过点击另一个按钮java执行按钮?
- 4. 如何按暂停按钮时暂停程序执行
- 5. 如何暂停HTML视频并通过循环自动恢复?
- 6. 如何通过点击按钮链接XML页面和布局?
- 7. 如何在Flash中实现点击和暂停/恢复?
- 8. 如何暂停和恢复wxThread与按钮事件
- 9. 如何在C#windowsForm中使用按钮单击时暂停和恢复命令
- 10. HTTPHandler:IRequiresSessionState暂停执行页面
- 11. 通过按键暂停和恢复线程
- 12. PhantomJS - 暂停/恢复JavaScript执行
- 13. 如何停止自动按钮点击F5和页面刷新?
- 14. 通过单击按钮运行并暂停GUI后台线程
- 15. 如何通过点击按钮打开新页面?
- 16. 通过按钮点击停止Ping.SendAsync WPF
- 17. 如何通过点击按钮执行一个PHP文件?
- 18. 如何暂停和恢复Jenkins构建
- 19. 如何暂停和恢复GameLoop?
- 20. 如何暂停和恢复JavaScript?
- 21. 如何暂停和恢复线程
- 22. 如何暂停和恢复音频
- 23. 如何暂停和恢复UIView动画?
- 24. NSOperation:如何暂停和恢复
- 25. 如何暂停和恢复录音
- 26. 如何暂停和恢复线程?
- 27. 如何暂停和恢复surfaceView线程
- 28. aframe /动画:如何暂停和恢复?
- 29. 重复AnimationDrawable和停止按钮点击
- 30. 暂停和恢复流
酷,所以你尝试过这么远吗? – G0dsquad
我试过setTimeout,但我没有得到我想要的。我想要暂停视频时获得的效果。我想要这个效果在我的html –