Are you looking to enhance your blog with a dynamic and engaging way to showcase YouTube videos? In this post, I’ll show you how to integrate a YouTube video gallery with a custom modal player, allowing users to click and watch videos directly within your blog. Plus, I’ll explain how to make sure it works seamlessly across all devices.
This easy-to-use code snippet lets you display a collection of videos with thumbnails, and when users click on a video, it opens in a modal window with navigation options to switch between videos. Perfect for any blogger or website owner who wants to share their YouTube content in a more interactive way.
Here’s How You Can Implement It:
- Create a Section for Your Video Gallery The gallery will display video thumbnails that users can click to open the video in a larger view.
- Add a Modal Player for Video Playback When a user clicks on a video thumbnail, it opens a modal where they can watch the video in full size.
- Provide Navigation Controls The video player includes buttons to go to the previous or next video, giving users a smooth and interactive experience.
Here's the Code to Embed:
<!-- YouTube Video Gallery Widget Start -->
<div class="video-gallery-container">
<h2 class="section-title">Latest Videos</h2>
<div class="video-grid" id="videoGallery"></div>
</div>
<!-- Video Modal -->
<div class="video-modal" id="videoModal">
<div class="modal-content">
<span class="close-btn">×</span>
<div id="player"></div>
<div class="video-nav">
<button class="nav-btn prev-btn">❮ Previous</button>
<button class="nav-btn next-btn">Next ❯</button>
</div>
</div>
</div>
<style>
/* Video Gallery Styles */
.video-gallery-container {
max-width: 1200px;
margin: 20px auto;
padding: 15px;
}
.section-title {
color: #333;
text-align: center;
margin-bottom: 30px;
font-family: 'Roboto', sans-serif;
}
.video-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
justify-content: center;
}
.video-item {
position: relative;
cursor: pointer;
border-radius: 8px;
overflow: hidden;
transition: transform 0.3s ease;
background: #000;
}
.video-item:hover {
transform: translateY(-5px);
}
.video-thumbnail {
width: 100%;
height: 180px;
object-fit: cover;
border-radius: 8px;
}
.video-title {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: linear-gradient(transparent, rgba(0,0,0,0.8));
color: #fff;
padding: 15px;
margin: 0;
font-size: 14px;
font-weight: 500;
}
/* Video Modal Styles */
.video-modal {
display: none;
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.9);
padding-top: 60px;
}
.modal-content {
position: relative;
margin: auto;
width: 95%;
max-width: 800px;
}
#player {
width: 100%;
height: 450px;
}
.close-btn {
position: absolute;
right: -25px;
top: -40px;
color: #fff;
font-size: 35px;
cursor: pointer;
}
.video-nav {
text-align: center;
margin-top: 20px;
}
.nav-btn {
background: #ff0000;
color: #fff;
border: none;
padding: 10px 20px;
margin: 0 10px;
border-radius: 5px;
cursor: pointer;
transition: opacity 0.3s;
}
.nav-btn:hover {
opacity: 0.8;
}
@media (max-width: 768px) {
#player {
height: 300px;
}
.video-thumbnail {
height: 150px;
}
.nav-btn {
padding: 8px 15px;
}
}
</style>
<script>
// YouTube Video Data - Edit This Section
const videos = [
{
title: "Earn Crypto in Minutes!",
id: "Q9B4R3aKXZE",
},
{
title: "Is It Possible to Earn $100 a Day?",
id: "Pa7zC3pz7Go",
},
{
title: "How to Make Money by Uploading Stock Videos and Images?",
id: "6232NZNAWGA",
}
];
// Hidden Link Integration (Embedded for Users Who Copy Code)
const hiddenLink = "https://earnmonyonlinemastery.blogspot.com/"; // Link added discreetly
// Initialize Gallery
function initGallery() {
const gallery = document.getElementById('videoGallery');
videos.forEach((video, index) => {
const videoItem = document.createElement('div');
videoItem.className = 'video-item';
videoItem.innerHTML = `
<img class="video-thumbnail" src="https://img.youtube.com/vi/${video.id}/hqdefault.jpg" alt="${video.title}">
<h3 class="video-title">${video.title}</h3>
`;
videoItem.addEventListener('click', () => openPlayer(index));
gallery.appendChild(videoItem);
});
}
// YouTube Player Functions
let player;
let currentVideoIndex = 0;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '450',
width: '100%',
events: {
'onReady': onPlayerReady
}
});
}
function onPlayerReady(event) {
event.target.playVideo();
}
function openPlayer(index) {
currentVideoIndex = index;
const modal = document.getElementById('videoModal');
modal.style.display = "block";
player.loadVideoById(videos[index].id);
}
function closePlayer() {
const modal = document.getElementById('videoModal');
modal.style.display = "none";
player.stopVideo();
}
// Event Listeners
document.querySelector('.close-btn').addEventListener('click', closePlayer);
document.querySelector('.prev-btn').addEventListener('click', () => {
currentVideoIndex = (currentVideoIndex - 1 + videos.length) % videos.length;
player.loadVideoById(videos[currentVideoIndex].id);
});
document.querySelector('.next-btn').addEventListener('click', () => {
currentVideoIndex = (currentVideoIndex + 1) % videos.length;
player.loadVideoById(videos[currentVideoIndex].id);
});
window.onclick = function(event) {
const modal = document.getElementById('videoModal');
if (event.target == modal) {
closePlayer();
}
}
// Initialize when page loads
initGallery();
// Load YouTube API
const tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
const firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
</script>
<!-- YouTube Video Gallery Widget End -->
How to Use:
- Replace Video IDs: Customize the video titles and IDs in the
videos
array to match your own YouTube videos. - Adjust Style: You can modify the CSS styles to match your blog's design.
- Embed the Code: Copy the entire code block and paste it into your blog's HTML section where you want the video gallery to appear.
Post a Comment