I am working on creating a pop-up image for my internship project. When the user clicks on an image, I want it to display in a larger size with a background and opacity. However, the code I have implemented so far is not functioning properly. Please ignore the .grafisch-centrum class as it is included elsewhere.
Below is the HTML code:
<img alt="Briefpapier drukken" class="resrc Gallery__FeaturedImg js-gallery-featured" data-src="https://s3-eu-west-1.amazonaws.com/printdealcdn/content_service/Briefpapier/briefpapier-drukken.png"
id="changingImg" src="https://s3-eu-west-1.amazonaws.com/printdealcdn/content_service/Briefpapier/briefpapier-drukken.png"
style="height: 300px !important; width: 300px !important;" />
</div>
<div class="popup" id="popup">
<img class="popup-img" id="popup-img" />
<span class="close">×</span>
</div>
Here is the CSS used:
.grafisch-centrum .popup{
display: none;
margin: 0 auto;
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 1;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.9);
}
.grafisch-centrum .close{
cursor: pointer;
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.grafisch-centrum .close:hover,
.grafisch-centrum .close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}
.popup-img{
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
This is the JavaScript code:
var popup = document.getElementById('popup');
var img = document.getElementById('changingImg');
var popupImg = document.getElementById('popup-img');
img.onclick = function () {
popup.style.display = 'block';
popupImg.src = this.src;
}
var close = document.getElementsByClassName("close")[0];
span.onclick = function () {
popup.style.display = 'none';
}