Javascript - 切换到另一个选项卡时暂停视频(在同一页面上)
Javascript - Pause video when changing to another tab (on the same page)
所以我设置了 3 个选项卡,几乎完全像这样 - https://www.w3schools.com/howto/howto_js_tabs.asp - 但每个选项卡下的内容是不同的视频 (HTML)。我的问题是,在更改选项卡后,前一个选项卡中的 video/audio 继续播放 - 除非我在切换选项卡之前暂停视频。对于与此类似的问题,我已经尝试了一些我见过的不同解决方案,但仍然找不到适合我页面的解决方案。
这是 HTML 我有 -
<!-- Tab links -->
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'BEE')" id="defaultOpen">BEE</button>
<button class="tablinks" onclick="openCity(event, 'CRAB')">CRAB</button>
<button class="tablinks" onclick="openCity(event, 'CROW')">CROW</button>
</div>
<!-- Tab content -->
<div id="BEE" class="tabcontent">
<div class="video">
<video width="852" height="480" controls>
<source src="bee.mov" type="video/mp4">
</video>
</div>
</div>
<div id="CRAB" class="tabcontent">
<div class="video">
<video width="852" height="480" controls>
<source src="crab.mov" type="video/mp4">
</video>
</div>
</div>
<div id="CROW" class="tabcontent">
<div class="video">
<video width="852" height="480" controls>
<source src="crow.mov" type="video/mp4">
</video>
</div>
</div>
以下是我尝试过的一些不同的解决方案 -
#1 - 我经常回来的主要内容 - Pause/stop videos into tabs when I clicked on a tab
#2 - How to pause a video when another video's play button is clicked
#3 - 我试过看看是否有办法可以将 space 栏切换为单击该栏上的任意位置 - https://teamtreehouse.com/community/hi-how-can-i-make-the-video-startstop-by-clicking-anywhere-on-the-video-icon-instead-of-targeting-the-play-button
我对 JS 还是很陌生,所以非常感谢任何帮助或建议!谢谢!
您可以在单击其中一个选项卡时暂停所有视频:
function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
tabcontent[i].querySelector("video").pause();
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
body {
font-family: Arial;
}
/* Style the tab */
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons inside the tab */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
.tabcontent .video {
width: 100%;
}
<h2>Tabs</h2>
<p>Click on the buttons inside the tabbed menu:</p>
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'London')">London</button>
<button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
<button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>
<div id="London" class="tabcontent">
<h3>London</h3>
<p>London is the capital city of England.</p>
<video controls>
<source src="https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4" type="video/mp4"/>
</video>
</div>
<div id="Paris" class="tabcontent">
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
<video controls>
<source src="https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4" type="video/mp4"/>
</video>
</div>
<div id="Tokyo" class="tabcontent">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
<video controls>
<source src="https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4" type="video/mp4"/>
</video>
</div>
所以我设置了 3 个选项卡,几乎完全像这样 - https://www.w3schools.com/howto/howto_js_tabs.asp - 但每个选项卡下的内容是不同的视频 (HTML)。我的问题是,在更改选项卡后,前一个选项卡中的 video/audio 继续播放 - 除非我在切换选项卡之前暂停视频。对于与此类似的问题,我已经尝试了一些我见过的不同解决方案,但仍然找不到适合我页面的解决方案。
这是 HTML 我有 -
<!-- Tab links -->
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'BEE')" id="defaultOpen">BEE</button>
<button class="tablinks" onclick="openCity(event, 'CRAB')">CRAB</button>
<button class="tablinks" onclick="openCity(event, 'CROW')">CROW</button>
</div>
<!-- Tab content -->
<div id="BEE" class="tabcontent">
<div class="video">
<video width="852" height="480" controls>
<source src="bee.mov" type="video/mp4">
</video>
</div>
</div>
<div id="CRAB" class="tabcontent">
<div class="video">
<video width="852" height="480" controls>
<source src="crab.mov" type="video/mp4">
</video>
</div>
</div>
<div id="CROW" class="tabcontent">
<div class="video">
<video width="852" height="480" controls>
<source src="crow.mov" type="video/mp4">
</video>
</div>
</div>
以下是我尝试过的一些不同的解决方案 - #1 - 我经常回来的主要内容 - Pause/stop videos into tabs when I clicked on a tab #2 - How to pause a video when another video's play button is clicked #3 - 我试过看看是否有办法可以将 space 栏切换为单击该栏上的任意位置 - https://teamtreehouse.com/community/hi-how-can-i-make-the-video-startstop-by-clicking-anywhere-on-the-video-icon-instead-of-targeting-the-play-button
我对 JS 还是很陌生,所以非常感谢任何帮助或建议!谢谢!
您可以在单击其中一个选项卡时暂停所有视频:
function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
tabcontent[i].querySelector("video").pause();
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
body {
font-family: Arial;
}
/* Style the tab */
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons inside the tab */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
.tabcontent .video {
width: 100%;
}
<h2>Tabs</h2>
<p>Click on the buttons inside the tabbed menu:</p>
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'London')">London</button>
<button class="tablinks" onclick="openCity(event, 'Paris')">Paris</button>
<button class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</button>
</div>
<div id="London" class="tabcontent">
<h3>London</h3>
<p>London is the capital city of England.</p>
<video controls>
<source src="https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4" type="video/mp4"/>
</video>
</div>
<div id="Paris" class="tabcontent">
<h3>Paris</h3>
<p>Paris is the capital of France.</p>
<video controls>
<source src="https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4" type="video/mp4"/>
</video>
</div>
<div id="Tokyo" class="tabcontent">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
<video controls>
<source src="https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4" type="video/mp4"/>
</video>
</div>