In this article lets learn simple steps to lazy load YouTube videos or any other videos. Once your website loads and if contains any YouTube or any other videos then the website loads longer than usual which has a huge impact on your users.
We will include the video thumbnail and on click of the thumbnail we will load the video. This drastically improves website loading and retaining most of your users
We will cover the following
NOTE: Code available at GitHub Lazy Load YouTube And Other Videos In Website
First let's add the HTML template with YouTube video thumbnail.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lazy Load YouTube Videos</title>
<!-- Video loading CSS style -->
<link rel="stylesheet" href="video-loader.css">
</head>
<body>
<div class="container">
<!-- Adding original source in data-src -->
<div class="video-wrapper" data-src="https://www.youtube.com/embed/R5ntdxOZ70E">
<!-- Loading video thumbnail -->
<img class="video-image" src="https://img.youtube.com/vi/R5ntdxOZ70E/hqdefault.jpg" />
</div>
</div>
<!-- Video loading JS script -->
<script src="video-loader.js"></script>
</body>
</html>
Responsive CSS to load youtube videos.
.container {
width: 480px;
margin: 0 auto;
}
.video-wrapper {
position: relative;
padding-top: 30px;
height: 100%;
overflow: hidden;
}
.video-image {
cursor: pointer;
}
.video-play-btn {
position: absolute;
}
.video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
We are looping through all the video wrapper and listening for the click
event to occur. When the click
event happens we will load the parent set data-src
actual video source.
function loadVideo(e){
var parent = this.parentNode;
parent.style.paddingBottom = "56.25%";
var dataSrc = parent.getAttribute('data-src');
parent.innerHTML = '<iframe class="video" src="'+ dataSrc +'?rel=0&autoplay=1&wmode=transparent" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen wmode="Opaque"></iframe>';
}
var videoImage = document.querySelectorAll('.video-wrapper .video-image');
for(var i = 0; i < videoImage.length; i++){
videoImage[i].addEventListener('click', loadVideo);
}
If you need any YouTube video thumbnail then you can use the following URLs. Make sure to just replace your YouTube video-id.
https://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
NOTE: Code available at GitHub Lazy Load YouTube And Other Videos In Website
I hope this article helped you. Please share it with your friends.
PHP Built-In Web Server & Testing Your Development Project In Mobile Without Any Software
What Is Method Chaining In PHP?
Create Zip File On The Fly With Streaming Download In PHP Laravel
Add Analytics To AMP (Accelerated Mobile Pages) HTML Pages
PHP file_put_contents failed to open stream: Permission denied
Free SSL Certificate For CPanel
Laravel 7.x Multiple Database Connections, Migrations, Relationships & Querying
Install Linux, Apache, MYSQL, PHP (LAMP Stack) on Ubuntu
Custom Validation Rules In PHP Laravel (Using Artisan Command)
Plain PHP Resumable Large File Uploads In Chunks Using FlowJs
What Is Laravel Resourceful Controllers?
Lazy Load YouTube And Other Videos In Website
Automate Repeating Tasks In Linux Server With Cronjobs
Sass or SCSS @mixin vs @extends vs Placeholder (%)
Install Packages Parallel For Faster Development In Composer