Interactive Image Gallery
Intermediate
A responsive image gallery with lightbox functionality, filtering, and smooth animations
Live Preview
Code Implementation
HTML
<div class="gallery-container">
<div class="gallery-header">
<h2>Interactive Image Gallery</h2>
<div class="filter-buttons">
<button class="filter-btn active" data-filter="all">All</button>
<button class="filter-btn" data-filter="nature">Nature</button>
<button class="filter-btn" data-filter="city">City</button>
<button class="filter-btn" data-filter="portrait">Portrait</button>
</div>
</div>
<div class="gallery-grid" id="galleryGrid">
<!-- Images will be dynamically loaded here -->
</div>
<button class="load-more-btn" id="loadMoreBtn">Load More</button>
</div>
<!-- Lightbox Modal -->
<div class="lightbox" id="lightbox">
<div class="lightbox-content">
<span class="close-btn" id="closeBtn">×</span>
<img class="lightbox-image" id="lightboxImage" src="" alt="">
<div class="lightbox-info">
<h3 id="lightboxTitle"></h3>
<p id="lightboxDescription"></p>
</div>
<button class="nav-btn prev-btn" id="prevBtn">❮</button>
<button class="nav-btn next-btn" id="nextBtn">❯</button>
</div>
</div>
CSS
.gallery-container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background: linear-gradient(135deg, #f8f9ff 0%, #ffffff 100%);
border-radius: 20px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}
.gallery-header {
text-align: center;
margin-bottom: 30px;
}
.gallery-header h2 {
color: #2c3e50;
font-size: 2.5rem;
margin-bottom: 20px;
font-weight: 300;
}
.filter-buttons {
display: flex;
justify-content: center;
gap: 10px;
flex-wrap: wrap;
}
.filter-btn {
padding: 10px 20px;
border: 2px solid #3498db;
background: transparent;
color: #3498db;
border-radius: 25px;
cursor: pointer;
transition: all 0.3s ease;
font-weight: 500;
}
.filter-btn:hover,
.filter-btn.active {
background: #3498db;
color: white;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(52, 152, 219, 0.3);
}
.gallery-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 20px;
margin-bottom: 30px;
}
.gallery-item {
position: relative;
border-radius: 15px;
overflow: hidden;
cursor: pointer;
transition: all 0.3s ease;
background: white;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}
.gallery-item:hover {
transform: translateY(-10px);
box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
}
.gallery-item img {
width: 100%;
height: 250px;
object-fit: cover;
transition: transform 0.3s ease;
}
.gallery-item:hover img {
transform: scale(1.1);
}
.gallery-item-overlay {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: linear-gradient(transparent, rgba(0, 0, 0, 0.8));
color: white;
padding: 20px;
transform: translateY(100%);
transition: transform 0.3s ease;
}
.gallery-item:hover .gallery-item-overlay {
transform: translateY(0);
}
.gallery-item-title {
font-size: 1.2rem;
font-weight: 600;
margin-bottom: 5px;
}
.gallery-item-category {
font-size: 0.9rem;
opacity: 0.8;
}
.load-more-btn {
display: block;
margin: 0 auto;
padding: 15px 30px;
background: linear-gradient(45deg, #3498db, #2980b9);
color: white;
border: none;
border-radius: 25px;
cursor: pointer;
font-size: 1.1rem;
font-weight: 500;
transition: all 0.3s ease;
}
.load-more-btn:hover {
transform: translateY(-2px);
box-shadow: 0 10px 20px rgba(52, 152, 219, 0.3);
}
.lightbox {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.9);
z-index: 1000;
animation: fadeIn 0.3s ease;
}
.lightbox-content {
position: relative;
max-width: 90%;
max-height: 90%;
margin: 5% auto;
background: white;
border-radius: 15px;
overflow: hidden;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}
.lightbox-image {
width: 100%;
height: auto;
max-height: 70vh;
object-fit: contain;
}
.lightbox-info {
padding: 20px;
background: white;
}
.lightbox-info h3 {
margin: 0 0 10px 0;
color: #2c3e50;
font-size: 1.5rem;
}
.lightbox-info p {
margin: 0;
color: #7f8c8d;
line-height: 1.6;
}
.close-btn {
position: absolute;
top: 15px;
right: 20px;
font-size: 2rem;
color: white;
cursor: pointer;
background: rgba(0, 0, 0, 0.5);
width: 40px;
height: 40px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
transition: background 0.3s ease;
}
.close-btn:hover {
background: rgba(0, 0, 0, 0.8);
}
.nav-btn {
position: absolute;
top: 50%;
transform: translateY(-50%);
background: rgba(0, 0, 0, 0.5);
color: white;
border: none;
padding: 15px 20px;
font-size: 1.5rem;
cursor: pointer;
transition: background 0.3s ease;
}
.prev-btn {
left: 20px;
border-radius: 0 10px 10px 0;
}
.next-btn {
right: 20px;
border-radius: 10px 0 0 10px;
}
.nav-btn:hover {
background: rgba(0, 0, 0, 0.8);
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
@media (max-width: 768px) {
.gallery-container {
padding: 15px;
}
.gallery-header h2 {
font-size: 2rem;
}
.gallery-grid {
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 15px;
}
.filter-buttons {
gap: 8px;
}
.filter-btn {
padding: 8px 16px;
font-size: 0.9rem;
}
}
JavaScript
document.addEventListener('DOMContentLoaded', () => {
const galleryGrid = document.getElementById('galleryGrid');
const loadMoreBtn = document.getElementById('loadMoreBtn');
const filterBtns = document.querySelectorAll('.filter-btn');
const lightbox = document.getElementById('lightbox');
const lightboxImage = document.getElementById('lightboxImage');
const lightboxTitle = document.getElementById('lightboxTitle');
const lightboxDescription = document.getElementById('lightboxDescription');
const closeBtn = document.getElementById('closeBtn');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
let currentImages = [];
let filteredImages = [];
let currentFilter = 'all';
let currentImageIndex = 0;
let displayedCount = 6;
// Sample image data
const imageData = [
{
id: 1,
src: 'https://picsum.photos/400/300?random=1',
title: 'Mountain Landscape',
description: 'Beautiful mountain scenery with crystal clear lakes',
category: 'nature'
},
{
id: 2,
src: 'https://picsum.photos/400/300?random=2',
title: 'City Skyline',
description: 'Modern city architecture at sunset',
category: 'city'
},
{
id: 3,
src: 'https://picsum.photos/400/300?random=3',
title: 'Portrait Study',
description: 'Artistic portrait with dramatic lighting',
category: 'portrait'
},
{
id: 4,
src: 'https://picsum.photos/400/300?random=4',
title: 'Forest Path',
description: 'Peaceful walking trail through dense forest',
category: 'nature'
},
{
id: 5,
src: 'https://picsum.photos/400/300?random=5',
title: 'Urban Street',
description: 'Busy street scene in downtown area',
category: 'city'
},
{
id: 6,
src: 'https://picsum.photos/400/300?random=6',
title: 'Creative Portrait',
description: 'Experimental portrait with unique composition',
category: 'portrait'
},
{
id: 7,
src: 'https://picsum.photos/400/300?random=7',
title: 'Ocean Waves',
description: 'Powerful waves crashing against rocky shore',
category: 'nature'
},
{
id: 8,
src: 'https://picsum.photos/400/300?random=8',
title: 'Night City',
description: 'City lights reflecting on wet streets',
category: 'city'
},
{
id: 9,
src: 'https://picsum.photos/400/300?random=9',
title: 'Fashion Portrait',
description: 'Stylish fashion photography with bold colors',
category: 'portrait'
}
];
// Initialize gallery
function initGallery() {
currentImages = [...imageData];
filterImages(currentFilter);
renderGallery();
}
// Filter images by category
function filterImages(category) {
if (category === 'all') {
filteredImages = [...currentImages];
} else {
filteredImages = currentImages.filter(img => img.category === category);
}
displayedCount = 6;
}
// Render gallery items
function renderGallery() {
galleryGrid.innerHTML = '';
const imagesToShow = filteredImages.slice(0, displayedCount);
imagesToShow.forEach((image, index) => {
const galleryItem = createGalleryItem(image, index);
galleryGrid.appendChild(galleryItem);
});
// Show/hide load more button
loadMoreBtn.style.display = displayedCount >= filteredImages.length ? 'none' : 'block';
}
// Create gallery item element
function createGalleryItem(image, index) {
const item = document.createElement('div');
item.className = 'gallery-item';
item.innerHTML = `
<img src="${image.src}" alt="${image.title}" loading="lazy">
<div class="gallery-item-overlay">
<div class="gallery-item-title">${image.title}</div>
<div class="gallery-item-category">${image.category}</div>
</div>
`;
item.addEventListener('click', () => openLightbox(index));
return item;
}
// Open lightbox
function openLightbox(index) {
currentImageIndex = index;
const image = filteredImages[index];
lightboxImage.src = image.src;
lightboxTitle.textContent = image.title;
lightboxDescription.textContent = image.description;
lightbox.style.display = 'block';
document.body.style.overflow = 'hidden';
}
// Close lightbox
function closeLightbox() {
lightbox.style.display = 'none';
document.body.style.overflow = 'auto';
}
// Navigate to previous image
function prevImage() {
currentImageIndex = currentImageIndex > 0 ? currentImageIndex - 1 : filteredImages.length - 1;
updateLightboxImage();
}
// Navigate to next image
function nextImage() {
currentImageIndex = currentImageIndex < filteredImages.length - 1 ? currentImageIndex + 1 : 0;
updateLightboxImage();
}
// Update lightbox image
function updateLightboxImage() {
const image = filteredImages[currentImageIndex];
lightboxImage.src = image.src;
lightboxTitle.textContent = image.title;
lightboxDescription.textContent = image.description;
}
// Event listeners
filterBtns.forEach(btn => {
btn.addEventListener('click', () => {
filterBtns.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
currentFilter = btn.dataset.filter;
filterImages(currentFilter);
renderGallery();
});
});
loadMoreBtn.addEventListener('click', () => {
displayedCount += 6;
renderGallery();
});
closeBtn.addEventListener('click', closeLightbox);
prevBtn.addEventListener('click', prevImage);
nextBtn.addEventListener('click', nextImage);
// Keyboard navigation
document.addEventListener('keydown', (e) => {
if (lightbox.style.display === 'block') {
switch(e.key) {
case 'Escape':
closeLightbox();
break;
case 'ArrowLeft':
prevImage();
break;
case 'ArrowRight':
nextImage();
break;
}
}
});
// Close lightbox when clicking outside image
lightbox.addEventListener('click', (e) => {
if (e.target === lightbox) {
closeLightbox();
}
});
// Initialize the gallery on page load
initGallery();
});