HTML Video pause()
Method: Pausing Video Playback
The HTML Video pause()
method is used to programmatically pause the currently playing video. It is an essential method for providing users with control over video playback on web pages. This guide will walk you through the syntax, practical examples, and real-world applications of the pause()
method.
What is the pause()
Method?
The pause()
method is a function available on HTML <video>
elements that halts video playback. When called, the video stops at its current frame until the play()
method is invoked, or the user manually resumes playback through the video controls.
Purpose of the pause()
Method
The primary purpose of the pause()
method is to allow developers to:
- Provide a programmatic way to pause video playback.
- Create custom video controls.
- Implement interactive video experiences.
- Synchronize video playback with other elements on the page.
Syntax
The syntax for using the pause()
method is straightforward:
videoElement.pause();
Where videoElement
is a reference to the HTML <video>
element in the DOM.
Parameters
The pause()
method does not accept any parameters.
Return Value
The pause()
method does not return any value (i.e., it returns undefined
).
Examples
Let’s explore some practical examples of how to use the pause()
method to control video playback.
Basic Pause Functionality
This example demonstrates how to pause a video when a button is clicked.
<video id="myVideoBasic" width="320" height="176" controls>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4" />
Your browser does not support HTML5 video.
</video>
<button id="pauseButtonBasic">Pause Video</button>
<script>
const videoBasic = document.getElementById("myVideoBasic");
const pauseButtonBasic = document.getElementById("pauseButtonBasic");
pauseButtonBasic.addEventListener("click", function() {
videoBasic.pause();
});
</script>
This code creates a video element and a button. When the button is clicked, the pause()
method is called on the video element, pausing the video.
Pause and Play Toggle
This example shows how to create a button that toggles between playing and pausing the video.
<video id="myVideoToggle" width="320" height="176" controls>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4" />
Your browser does not support HTML5 video.
</video>
<button id="toggleButton">Play/Pause</button>
<script>
const videoToggle = document.getElementById("myVideoToggle");
const toggleButton = document.getElementById("toggleButton");
toggleButton.addEventListener("click", function() {
if (videoToggle.paused) {
videoToggle.play();
toggleButton.textContent = "Pause";
} else {
videoToggle.pause();
toggleButton.textContent = "Play";
}
});
</script>
This code checks if the video is currently paused. If it is, it plays the video and changes the button text to “Pause.” If it is playing, it pauses the video and changes the button text to “Play.”
Pause on Visibility Change
This example demonstrates how to pause the video when the user navigates away from the tab or minimizes the browser window.
<video id="myVideoVisibility" width="320" height="176" controls>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4" />
Your browser does not support HTML5 video.
</video>
<script>
const videoVisibility = document.getElementById("myVideoVisibility");
document.addEventListener("visibilitychange", function() {
if (document.hidden) {
videoVisibility.pause();
}
});
</script>
This code listens for the visibilitychange
event, which is triggered when the visibility of the page changes. If the page is hidden (e.g., the user switches to another tab), the video is paused.
Pause on Scroll
This example shows how to pause the video when it scrolls out of view.
<video id="myVideoScroll" width="320" height="176" controls style="margin-bottom: 1000px;">
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4" />
Your browser does not support HTML5 video.
</video>
<script>
const videoScroll = document.getElementById("myVideoScroll");
function isElementInViewport(el) {
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
window.addEventListener("scroll", function() {
if (!isElementInViewport(videoScroll)) {
videoScroll.pause();
}
});
</script>
This code defines a function isElementInViewport
that checks if the video element is currently in the viewport. The scroll
event listener calls this function and pauses the video if it is not in the viewport. A bottom margin is added to the video to allow for scrolling.
Pause all other videos when one plays
This example shows how to pause all other videos on the page when one video starts playing, ensuring that only one video plays at a time.
<video id="video1" width="320" height="176" controls>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
<video id="video2" width="320" height="176" controls>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
<script>
const videos = document.querySelectorAll('video');
videos.forEach(video => {
video.addEventListener('play', function() {
videos.forEach(otherVideo => {
if (otherVideo !== video) {
otherVideo.pause();
}
});
});
});
</script>
This code selects all video elements on the page and adds a ‘play’ event listener to each. When a video starts playing, the listener pauses all other videos.
Pause Video on Button Click
This example combines basic JavaScript with the HTML <video>
element to create a pause button that controls the video playback.
<video id="myVideo" width="320" height="240" controls>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
<button onclick="pauseVideo()" type="button">Pause Video</button>
<script>
function pauseVideo() {
var video = document.getElementById("myVideo");
video.pause();
}
</script>
In this setup, a button with an onclick
event calls a JavaScript function pauseVideo
. This function retrieves the video element by its ID and calls the pause()
method, effectively stopping the video playback.
Dynamic Pause Button
This example extends the previous one by dynamically changing the button text based on the video state.
<video id="dynamicVideo" width="320" height="240" controls>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
<button id="pauseButton" onclick="togglePause()" type="button">Pause Video</button>
<script>
function togglePause() {
var video = document.getElementById("dynamicVideo");
var button = document.getElementById("pauseButton");
if (video.paused) {
video.play();
button.textContent = "Pause Video";
} else {
video.pause();
button.textContent = "Play Video";
}
}
</script>
Here, the togglePause
function checks if the video is paused. If it is, the video is played, and the button text changes to “Pause Video”. If the video is playing, it is paused, and the button text changes to “Play Video”, providing a more intuitive user interface.
Pause Video on Scroll
This example demonstrates how to pause a video when it scrolls out of the viewport.
<video id="videoOnScroll" width="320" height="240" controls style="margin-bottom: 1000px;">
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
<script>
var video = document.getElementById("videoOnScroll");
function isElementOutOfViewport(el) {
var rect = el.getBoundingClientRect();
return (rect.bottom < 0 || rect.top > window.innerHeight);
}
window.addEventListener('scroll', function() {
if (isElementOutOfViewport(video)) {
video.pause();
}
});
</script>
In this example, the isElementOutOfViewport
function checks if the video element is out of the viewport. The scroll
event listener pauses the video when it scrolls out of view, optimizing the user experience.
Real-World Applications of the pause()
Method
The pause()
method is used in various real-world applications, including:
- Custom Video Players: Implementing custom play/pause controls.
- Interactive Tutorials: Pausing videos at specific points for user interaction.
- Advertising: Pausing video ads when the user switches to another tab.
- Accessibility: Providing pause functionality for users with disabilities.
Browser Support
The pause()
method is supported by all modern web browsers. Here is a general overview:
- Chrome: Supported
- Edge: Supported
- Firefox: Supported
- Safari: Supported
- Opera: Supported
Conclusion
The HTML Video pause()
method is a fundamental tool for controlling video playback on web pages. By using this method, developers can create custom video controls, implement interactive video experiences, and ensure that videos behave as expected in various scenarios. With the examples and explanations provided in this guide, you should now have a solid understanding of how to use the pause()
method effectively in your web development projects. 🚀