Reputation: 21
I would like to show a modal/popup image when I click on an image in my gallery. I took the example from https://www.w3schools.com/howto/howto_css_modal_images.asp which is easy enough to implement. However, because I would like another overlay effect (image goes dark-transparent and shows a text overlay when hovering), I have to wrap my modal-trigger in a div.
I placed the id="myImg"
into the div so I can at least trigger the modal. It was supposed to be in the img-tag like so: <img id="myImg" src="images/kayla-good-qual.jpg" alt="Test Image Modal" style="width: 100%">
. This wouldn't trigger the modal at all, unless I would remove the class="img-container"
.
For my solution above, I assume the javascript can't find the image src and alt? I am a noob in Javascript and I couldn't figure out how to amend the code - if that's the mistake.
So, what to do so I can keep my image and text overlay/hover and at the same time have the image modal working?
Below my JS, CSS and HTML code.
Thanks heaps!
var modal = document.getElementById("myModal");
var img = document.getElementById("myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
var span = document.getElementsByClassName("close")[0];
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
span.onclick = function() {
modal.style.display = "none";
}
.img-container {
position: relative;
display: inline-block;
}
.img-container:after {
cursor: pointer;
content: attr(data-content);
color: #fff;
padding: 4px 0 0 8px;
position:absolute;
width:100%; height:100%;
top:0; left:0;
background:rgba(0,0,0,0.6);
opacity:0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
}
.img-container:hover:after {
opacity:1;
}
/* Style the Image Used to Trigger the Modal */
/*#myImg {
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {
opacity: 0.7;
}*/
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}
/* Modal Content (Image) */
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
/* Caption of Modal Image (Image Text) - Same Width as the Image */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add Animation - Zoom in the Modal */
.modal-content, #caption {
animation-name: zoom;
animation-duration: 0.6s;
}
@keyframes zoom {
from {transform:scale(0)}
to {transform:scale(1)}
}
/* The Close Button */
.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;
}
/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
.modal-content {
width: 100%;
}
}
<div id="myImg" data-content="Text added on hover" class="img-container">
<img src="images/kayla-good-qual.jpg" alt="Test Image Modal" style="width: 100%">
</div>
<div id="myModal" class="modal">
<span class="close">×</span>
<img class="modal-content" id="img01">
<div id="caption"></div>
</div>
Upvotes: 1
Views: 1326
Reputation: 21
I have found a solution to my own problem, althoug it might be quite bad code:
id="myImgSrc"
so it looks like this: <img id="myImgSrc" src="images/kayla-good-qual.jpg" alt="Test Image Modal" style="width: 100%">
this
to document.getElementById("myImgSrc")
If anyone knows a cleaner solution, I am all ears (and eyes)!
Upvotes: 0