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;
// Datos de ejemplo de imágenes
const imageData = [
{
id: 1,
src: 'https://picsum.photos/400/300?random=1',
title: 'Paisaje de Montaña',
description: 'Hermoso paisaje montañoso con lagos cristalinos',
category: 'nature'
},
{
id: 2,
src: 'https://picsum.photos/400/300?random=2',
title: 'Horizonte de la Ciudad',
description: 'Arquitectura moderna de la ciudad al atardecer',
category: 'city'
},
{
id: 3,
src: 'https://picsum.photos/400/300?random=3',
title: 'Estudio de Retrato',
description: 'Retrato artístico con iluminación dramática',
category: 'portrait'
},
{
id: 4,
src: 'https://picsum.photos/400/300?random=4',
title: 'Sendero del Bosque',
description: 'Sendero pacífico a través de un bosque denso',
category: 'nature'
},
{
id: 5,
src: 'https://picsum.photos/400/300?random=5',
title: 'Calle Urbana',
description: 'Escena de calle concurrida en el centro de la ciudad',
category: 'city'
},
{
id: 6,
src: 'https://picsum.photos/400/300?random=6',
title: 'Retrato Creativo',
description: 'Retrato experimental con composición única',
category: 'portrait'
},
{
id: 7,
src: 'https://picsum.photos/400/300?random=7',
title: 'Olas del Océano',
description: 'Poderosas olas rompiendo contra la costa rocosa',
category: 'nature'
},
{
id: 8,
src: 'https://picsum.photos/400/300?random=8',
title: 'Ciudad Nocturna',
description: 'Luces de la ciudad reflejándose en calles mojadas',
category: 'city'
},
{
id: 9,
src: 'https://picsum.photos/400/300?random=9',
title: 'Retrato de Moda',
description: 'Fotografía de moda elegante con colores audaces',
category: 'portrait'
}
];
// Inicializar galería
function initGallery() {
currentImages = [...imageData];
filterImages(currentFilter);
renderGallery();
}
// Filtrar imágenes por categoría
function filterImages(category) {
if (category === 'all') {
filteredImages = [...currentImages];
} else {
filteredImages = currentImages.filter(img => img.category === category);
}
displayedCount = 6;
}
// Renderizar elementos de la galería
function renderGallery() {
galleryGrid.innerHTML = '';
const imagesToShow = filteredImages.slice(0, displayedCount);
imagesToShow.forEach((image, index) => {
const galleryItem = createGalleryItem(image, index);
galleryGrid.appendChild(galleryItem);
});
// Mostrar/ocultar botón cargar más
loadMoreBtn.style.display = displayedCount >= filteredImages.length ? 'none' : 'block';
}
// Crear elemento de la galería
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;
}
// Abrir 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';
}
// Cerrar lightbox
function closeLightbox() {
lightbox.style.display = 'none';
document.body.style.overflow = 'auto';
}
// Navegar a imagen anterior
function prevImage() {
currentImageIndex = currentImageIndex > 0 ? currentImageIndex - 1 : filteredImages.length - 1;
updateLightboxImage();
}
// Navegar a siguiente imagen
function nextImage() {
currentImageIndex = currentImageIndex < filteredImages.length - 1 ? currentImageIndex + 1 : 0;
updateLightboxImage();
}
// Actualizar imagen del lightbox
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);
// Navegación por teclado
document.addEventListener('keydown', (e) => {
if (lightbox.style.display === 'block') {
switch(e.key) {
case 'Escape':
closeLightbox();
break;
case 'ArrowLeft':
prevImage();
break;
case 'ArrowRight':
nextImage();
break;
}
}
});
// Cerrar lightbox al hacer clic fuera de la imagen
lightbox.addEventListener('click', (e) => {
if (e.target === lightbox) {
closeLightbox();
}
});
// Inicializar la galería al cargar la página
initGallery();
});