【问题标题】:Javascript - Pause video when changing to another tab (on the same page)Javascript - 切换到另一个选项卡时暂停视频(在同一页面上)
【发布时间】:2021-05-25 06:36:40
【问题描述】:

所以我设置了 3 个标签,几乎与此类似 - https://www.w3schools.com/howto/howto_js_tabs.asp - 但每个标签下的内容是不同的视频 (HTML)。我的问题是,在更改标签后,前一个标签中的视频/音频会继续播放 - 除非我在切换标签之前暂停视频。对于与此类似的问题,我尝试了几种不同的解决方案,但仍然找不到适用于我的页面的解决方案。

这是我的 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 - 我试着看看是否有办法可以将空格键切换到单击此空格键的任意位置 - 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 还是很陌生,因此非常感谢任何帮助或建议!谢谢!

【问题讨论】:

  • 您需要向我们展示您尝试过的不同解决方案才能获得答案。

标签: javascript html css video


【解决方案1】:

当点击其中一个标签时,您可以暂停所有视频:

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 2016-06-29
    • 2011-05-21
    • 2012-07-18
    • 2014-11-25
    • 1970-01-01
    • 2021-09-13
    相关资源
    最近更新 更多