javascript - javascript中不同视频的两个按钮冲突
问题描述
当您按下 START 时,Video1 应该启动,当您按下 STOP 按钮时,Video2 应该启动并隐藏 Video1。我的问题是 Video1 的块隐藏并且 Video2 启动,但是当您按下 START 按钮时,Video1 没有启动。如果您要删除启动 Video2 的部分脚本,则 Video1 将启动并隐藏工作。
function hideLayer(ObHide) {
document.getElementById(ObHide).style.visibility = "hidden";
}
function showLayer(ObShow) {
document.getElementById(ObShow).style.visibility = "visible";
}
// Master function, encapsulates all functions
function init() {
var video = document.getElementById("Video1");
if (video.canPlayType) {
document.getElementById("buttonbar1").style.display = "block";
document.getElementById("play").addEventListener("click", vidplay, false);
function vidplay(evt) {
if (video.src == "vid.mp4") { // on first run, src is empty, go get file
getVideo();
}
button = evt.target; // get the button id to swap the text based on the state
if (video.paused) { // play the file, and display pause symbol
video.play();
button.textContent = "START";
}
}
} // end of runtime
}
// end of master
// Master function, encapsulates all functions
function init() {
var video = document.getElementById("Video2");
if (video.canPlayType) {
document.getElementById("buttonbar").style.display = "block";
document.getElementById("stop").addEventListener("click", vidplay, false);
function vidplay(evt) {
if (video.src == "vid.mp4") { // on first run, src is empty, go get file
getVideo();
}
button = evt.target; // get the button id to swap the text based on the state
if (video.paused) { // play the file, and display pause symbol
video.play();
button.textContent = "STOP";
}
}
}
} // end of runtime
// end of master
* {
padding: 0;
margin: 0;
}
html {
height: 300px;
}
body {
height: 600px;
padding: 0;
margin: 0;
}
.wrapper {
min-height: 100%;
height: auto;
margin: -50px auto 0;
width: 100%;
}
.header {
height: 50px;
background-color: #2f4f4f;
padding-top: 50px;
}
.content {
margin: 3%;
width: 60%;
height: auto;
}
h2 {
margin: 0.5%;
color: #fff;
}
#Video1 {
width: 80%;
height: auto;
margin-top: -75%;
background-size: cover;
border: 2px solid;
float: inherit;
}
#Video2 {
width: 80%;
height: auto;
margin-bottom: -2%;
background-size: cover;
border: 2px solid;
float: inherit;
}
.footer {
height: 50px;
background-color: #2f4f4f;
margin: auto 0;
}
.b-play {
margin-left: 53%;
margin-top: -100%;
float: inherit;
}
#play {
width: 14%;
height: auto;
background: #229b24;
text-align: center;
font-size: 250%;
font-family: 'Pacifico', cursive;
color: #fff;
}
.b-stop {
margin-left: 53%;
float: inherit;
}
#stop {
width: 14%;
height: auto;
background: red;
text-align: center;
font-size: 250%;
font-family: 'Pacifico', cursive;
color: #fff;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title> PHISIC</title>
</head>
<body onload="init()">
<div class="wrapper">
<div class="header">
<h2>Phisic Model</h2>
</div>
<div class="content">
<!-- <video id="Video1" style="border: 1px solid blue;" height="624" width="880" src="vid2.mp4">
</video> -->
<div><video id="Video2" style="border: 1px solid blue;" height="624" width="880" src="vid.mp4" type="video/mp4">
</video></div>
<div id="Layer1"><video id="Video1" style="border: 1px solid blue;"><source src="vid2.mp4" type="video/mp4"></video></div>
<div id="buttonbar" style="display: none;">
</div>
<!-- <div id="buttonbar" style="display: none;">
</div> -->
<input class="b-play" id="play" type="button" name="ly1" value="START" onClick="showLayer('Layer1')">
<!-- <div class="b-play"><button id="play" >START</button></div><br> -->
<input class="b-stop" id="stop" type="button" name="ly1" value="STOP" onClick="hideLayer('Layer1')">
<!-- <div class="b-stop"><button id="stop" >STOP</button></div> -->
</div>
<div class="footer"></div>
</div>
</body>
</html>
解决方案
发生这种情况是因为您有两个 init 函数。最后一个是覆盖最初的定义。
单人试试
function hideLayer(ObHide) {
document.getElementById(ObHide).style.visibility = "hidden";
}
function showLayer(ObShow) {
document.getElementById(ObShow).style.visibility = "visible";
}
// Master function, encapsulates all functions
function init() {
var video1 = document.getElementById("Video1");
var video2 = document.getElementById("Video2");
if (video1.canPlayType && video2.canPlayType) {
document.getElementById("play").addEventListener("click", vidplay1, false);
document.getElementById("stop").addEventListener("click", vidplay2, false);
function vidplay1(evt) {
button = evt.target; // get the button id to swap the text based on the state
if (video1.paused) { // play the file, and display pause symbol
video1.play();
video2.pause();
button.textContent = "START";
}
}
function vidplay2(evt) {
button = evt.target; // get the button id to swap the text based on the state
if (video2.paused) { // play the file, and display pause symbol
video2.play();
video1.pause();
}
}
} // end of runtime
}
这应该工作
推荐阅读
- python - 在 python 中,结果文件不包含所有这些值
- c - 我需要帮助弄清楚这段 C 代码的作用
- java - 将我在字符串中的内容输入程序
- android - java.lang.IllegalArgumentException:无法创建没有颜色空间的位图
- node.js - NextJS 部署 - 如何在 Ubuntu 服务器上简单地部署 NextJS,如 NodeJS?
- node.js - NodeJS + Apache Proxy,在路由安全的 websocket 时遇到问题(请提供 apache 配置帮助)
- python - Pyspark基于具有多个条件的其他列与列表或集合创建新列
- javascript - 我试图弄清楚如何使用 addEventListener 仅在用户选择某个选项时显示
- angular - GCP 中定义的防火墙规则,但无法访问 Angular 默认应用
- r - 带有ui和服务器的shinyApp在单独的文件中?