Tell us what’s happening:
Everything work but : While the lightbox is up, clicking on the picture makes it display go to none. I hoped i could get around that and make it so the lighbox only disapear if I click on the cross or the lightbox itself.
How could I do that ?
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="styles.css" rel="stylesheet">
<title>Lightbox Viewer</title>
</head>
<body>
<h1>Gallery of Randomness</h1>
<div class="gallery">
<img class="gallery-item" src="https://cdn.freecodecamp.org/curriculum/labs/stonehenge-thumbnail.jpg" alt="Random rocks vertically put">
<img class="gallery-item" src="https://cdn.freecodecamp.org/curriculum/labs/storm-thumbnail.jpg" alt="Random dark looking sky with some horses and building">
<img class="gallery-item" src="https://cdn.freecodecamp.org/curriculum/labs/trees-thumbnail.jpg" alt="Random sheeps traversing a random village of some random years before road">
</div>
<div class="lightbox">
<button id="close-btn">×</button>
<img id="lightbox-image">
</div>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
*{
box-sizing: border-box;
}
body{
background-color: lightgrey;
text-align:center;
}
.gallery{
width: 90%;
display:flex;
flex-wrap: no-wrap;
justify-content: center;
gap:10px;
margin: 0 auto;
}
.gallery-item{
width:30%;
cursor:pointer;
}
.lightbox{
display:none;
background-color: rgba(120, 120, 120, 0.5);
position:fixed;
width: 100vw;
height: 100vh;
top: 0;
left: 0;
}
.lightbox img{
margin: 0 auto;
max-width: 80%;
height: auto;
object-fit:contain;
}
#close-btn{
display:flex;
color: white;
font-size: 18px;
background-color: rgb(83, 83, 83);
border-radius: 20px;
justify-content:center;
align-items: center;
position:fixed;
width: 30px;
height:30px;
right: 30px;
top:30px;
border:none;
}
#close-btn:hover{
background-color:lightgrey;
color:red;
cursor:pointer;
}
/* file: script.js */
const lightbox = document.querySelector(".lightbox");
const galleryItem = document.querySelectorAll(".gallery-item");
const boxImg = document.getElementById("lightbox-image");
const closeBtn = document.getElementById("close-btn")
function biggerImg(img){
//Show lightbox
lightbox.style.display = "flex";
//Give lightboxIMG its source;
let newURL = img.src.replace("-thumbnail", "");
boxImg.setAttribute("src", newURL)
}
function closeBL(){
//display lightbox to none
lightbox.style.display = "none";
}
//gallery Item Event
galleryItem.forEach(img => img.addEventListener("click", () => biggerImg(img)));
//Close btn and Lightbox Event => closeBL
lightbox.addEventListener("click", () => closeBL()).stopPropagation();
closeBtn.addEventListener("click", () => closeBL()).stopPropagation();
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:148.0) Gecko/20100101 Firefox/148.0
Challenge Information:
Build a Lightbox Viewer - Build a Lightbox Viewer