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();
});