I'm new to javascript and I'm looking for help with a simple task. I want to create a code that when clicking on an image, it will open in a modal. This feature is important for viewing full-size images on my portfolio. Although there are no errors showing in the console, nothing happens when I click on the image.
Here is the javascript code:
var modal = document.getElementById("myModal");
var srcImg = document.getElementsByClassName("showFull");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
srcImg.onclick = function () {
modal.style.display = "block";
modalImg.src = this.src;
modalImg.alt = this.alt;
captionText.innerHTML = this.alt;
}
var exit = document.getElementsByClassName("close")[0];
exit.onclick = function () {
modal.style.display = "none";
};
And here is the HTML code:
<div class="mySlides fade">
<img class="slide showFull" src="images/mikesHarder.jpg alt="caption 123" style="width:100%" />
</div>
<div class="mySlides fade">
<img class="slide showFull" src="images/Going-Green.jpg" alt="caption 123" style="width:100%" />
</div>
<div id="myModal" class="modal">
<span class="close">×</span>
<img id="img01" class="modal-content"/>
<div id="caption"></div>
</div>
Lastly, we have the CSS code:
.showFull {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
.showFull:hover { opacity: 0.9; }
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0, 0, 0);
background-color: rgba(0, 0, 0, 0.8);
}
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 480px;
}
.modal-content {
animation-name: zoom;
animation-duration: 0.6s;
}
@keyframes zoom {
from {transform: scale(0)}
to {transform: scale(1)}
}
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}
@media only screen and (max-width: 700px) {
.modal-content {
width: 100%;
}
}