Javascript - Pause video when changing to another tab (on the same page)
You can pause all videos when one of the tabs is clicked:
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>
Pause HTML Video on exiting a tab
You can simply loop through each video and pause it inside of your function.
Credit: How can I pause ALL videos (html5) at once?
$('ul.tabs li').click(function() { $('video').each(function() { $(this).get(0).pause(); }); var tab = $(this).data('tab');
$('ul.tabs li').removeClass('current'); $('.tab-content').removeClass('current');
$(this).addClass('current'); $(this).parent().parent().parent().children("#"+tab).addClass('current');});
section { background: #fff;}
ul.tabs { margin: 0; padding: 0; list-style: none; background: #ccc;}
.tab-link { background: none; color: #fff; display: inline-block; padding: 10px; cursor: pointer; font-size: 16px;}
.tabs .tab-link.current { background: #ccc;}
.tab-content { display: none; background: #ccc;}
.tab-content.current { display: block;}
video { width: 100%; height: 400px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
<nav> <ul class="tabs"> <li class="tab-link current" data-tab="first"> <a href="#tab-1">One</a> </li> <li class="tab-link" data-tab="sec"> <a href="#tab-2">Two</a> </li> </ul> </nav>
<div id="first" class="tab-content current"> <video controls> <source type="video/mp4" src="http://techslides.com/demos/sample-videos/small.mp4"> </video> </div> <div id="sec" class="tab-content"> <video controls> <source type="video/mp4" src="http://techslides.com/demos/sample-videos/small.mp4"> </video> </div>
</section>
How to stop video in tab?
DEMO — Switching tabs pauses any playing videos.
By using the YouTube Player API, you can play/pause/stop videos.
<script src="//www.youtube.com/iframe_api"></script>
<iframe width="510" height="287" src="//www.youtube.com/embed/VIDEO_ID?enablejsapi=1" frameborder="0" allowfullscreen></iframe>
<script>
$(function(){
$('iframe[src*="//www.youtube.com/embed/"]').each(function(i) {
this.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
});
});
</script>
Note the ?enablejsapi=1
query string appended to the YouTube embed URL in the iframe
.
Pause video on Tab switch in browser but should continue Playing when minimized the browser window
document.addEventListener("visibilitychange", function() {
if (document.hidden){
console.log("Browser tab is hidden")
} else {
console.log("Browser tab is visible")
}
});
Related Topics
React Button Onclick Redirect Page
Check If Second String Is Rotation of Another String
How to Change Value of Object Which Is Inside an Array Using JavaScript or Jquery
How to Get Full Path of Selected File on Change of <Input Type='File'> Using Javascript, Jquery-Ajax
Display Images in React Using Jsx Without Import
Changing the Browser Zoom Level
Passing Variable from One Js to Another Js File in Nodejs
Accessing Ejs Variable in JavaScript Logic
Find Minimum Numbers of Currency for Required Amount
How to Add Active Class/State for Li Element in Reactjs
How to Show Which Question a User Is on When Taking My Quiz
Converting String Date in React/Javascript
Wait for Asynchronous Functions to Finish in Angular
How to Hide Select Options With Javascript (Cross Browser)
Detect If a Field Is Updated With JavaScript or Jquery
Jquery, Line Break in Textarea.. Don't Get \N and Get ↵