javascript - How set Modal popup multiple image -
i'm try use w3s show multi images in modal popup:
// modal var modal = document.getelementbyid('mymodal'); // image , insert inside modal - use "alt" text caption var img = document.getelementbyid('myimg'); var modalimg = document.getelementbyid("img01"); var captiontext = document.getelementbyid("caption"); img.onclick = function(){ modal.style.display = "block"; modalimg.src = this.src; captiontext.innerhtml = this.alt; } // <span> element closes modal var span = document.getelementsbyclassname("close")[0]; // when user clicks on <span> (x), close modal span.onclick = function() { modal.style.display = "none"; }
/* style image used trigger modal */ #myimg { border-radius: 5px; cursor: pointer; transition: 0.3s; } #myimg:hover {opacity: 0.7;} /* modal (background) */ .modal { display: none; /* hidden default */ position: fixed; /* stay in place */ z-index: 1; /* sit on top */ padding-top: 100px; /* location of 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 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 modal */ .modal-content, #caption { -webkit-animation-name: zoom; -webkit-animation-duration: 0.6s; animation-name: zoom; animation-duration: 0.6s; } @-webkit-keyframes zoom { {-webkit-transform:scale(0)} {-webkit-transform:scale(1)} } @keyframes zoom { {transform:scale(0)} {transform:scale(1)} } /* 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 screen , (max-width: 700px){ .modal-content { width: 100%; } }
<!-- trigger modal --> <img id="myimg" src="img_fjords.jpg" alt="trolltunga, norway" width="300" height="200"> <!-- modal --> <div id="mymodal" class="modal"> <!-- close button --> <span class="close" onclick="document.getelementbyid('mymodal').style.display='none'">×</span> <!-- modal content (the image) --> <img class="modal-content" id="img01"> <!-- modal caption (image text) --> <div id="caption"></div> </div>
it work fine 1 image.
on site have pages need show more image, , if set code other image, popup work first , not other images.
how set correctly html , javascript?
this implementation not work referring same dom element id. can try jsfiddle uses magnific popup
plugin make multiple image modal. easier implement well.
here example of magnific popup: https://jsfiddle.net/8f60f93t/2/
Comments
Post a Comment