Javascript Lightbox Tutorial Development Tutorials, Guides & Insights
Unlock 1+ expert-curated javascript lightbox tutorial tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your javascript lightbox tutorial skills on DeveloperBreeze.
Adblocker Detected
It looks like you're using an adblocker. Our website relies on ads to keep running. Please consider disabling your adblocker to support us and access the content.
JavaScript Image Gallery with Lightbox
Code August 08, 2024
javascript
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset=UTF-8>
<meta name=viewport content="width=device-width, initial-scale=1.0">
<title>Image Gallery with Lightbox</title>
<style>
.gallery {
display: flex;
flex-wrap: wrap;
gap: 10px;
justify-content: center;
}
.gallery img {
width: 150px;
height: auto;
cursor: pointer;
transition: transform 0.3s ease;
}
.gallery img:hover {
transform: scale(1.05);
}
.lightbox {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.8);
justify-content: center;
align-items: center;
}
.lightbox img {
max-width: 90%;
max-height: 90%;
}
</style>
</head>
<body>
<div class=gallery>
<img src=https://via.placeholder.com/300x200 alt="Image 1" onclick=openLightbox(this)>
<img src=https://via.placeholder.com/300x200 alt="Image 2" onclick=openLightbox(this)>
<img src=https://via.placeholder.com/300x200 alt="Image 3" onclick=openLightbox(this)>
<img src=https://via.placeholder.com/300x200 alt="Image 4" onclick=openLightbox(this)>
</div>
<div class=lightbox id=lightbox onclick=closeLightbox()>
<img src alt="Lightbox Image" id=lightboxImage>
</div>
<script>
function openLightbox(image) {
const lightbox = document.getElementById('lightbox');
const lightboxImage = document.getElementById('lightboxImage');
lightboxImage.src = image.src;
lightbox.style.display = 'flex';
}
function closeLightbox() {
const lightbox = document.getElementById('lightbox');
lightbox.style.display = 'none';
}
</script>
</body>
</html>
- HTML Structure: The gallery is composed of image elements wrapped in a container. Each image has an
onclickevent that triggers the lightbox.