javascript - Multiple Lightbox (Modal Image Gallery) on 1 page -
i create lightbox effect without downloading anything. found nice solution using modal/slideshow lightbox , works well. have more 1 on page , can't work. here link example , code below. https://www.w3schools.com/howto/howto_js_lightbox.asp
<!doctype html> <html> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body { font-family: verdana, sans-serif; margin: 0; } * { box-sizing: border-box; } .row > .column { padding: 0 8px; } .row:after { content: ""; display: table; clear: both; } .column { float: left; width: 25%; } /* modal (background) */ .modal { display: none; position: fixed; z-index: 1; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: black; } /* modal content */ .modal-content { position: relative; background-color: #fefefe; margin: auto; padding: 0; width: 90%; max-width: 1200px; } /* close button */ .close { color: white; position: absolute; top: 10px; right: 25px; font-size: 35px; font-weight: bold; } .close:hover, .close:focus { color: #999; text-decoration: none; cursor: pointer; } .myslides { display: none; } .cursor { cursor: pointer } /* next & previous buttons */ .prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; padding: 16px; margin-top: -50px; color: white; font-weight: bold; font-size: 20px; transition: 0.6s ease; border-radius: 0 3px 3px 0; user-select: none; -webkit-user-select: none; } /* position "next button" right */ .next { right: 0; border-radius: 3px 0 0 3px; } /* on hover, add black background color little bit see-through */ .prev:hover, .next:hover { background-color: rgba(0, 0, 0, 0.8); } /* number text (1/3 etc) */ .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } img { margin-bottom: -4px; } .caption-container { text-align: center; background-color: black; padding: 2px 16px; color: white; } .demo { opacity: 0.6; } .active, .demo:hover { opacity: 1; } img.hover-shadow { transition: 0.3s } .hover-shadow:hover { box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19) } </style> <body> <h2 style="text-align:center">lightbox</h2> <div class="row"> <div class="column"> <img src="img_nature.jpg" style="width:100%" onclick="openmodal();currentslide(1)" class="hover-shadow cursor"> </div> <div class="column"> <img src="img_fjords.jpg" style="width:100%" onclick="openmodal();currentslide(2)" class="hover-shadow cursor"> </div> <div class="column"> <img src="img_mountains.jpg" style="width:100%" onclick="openmodal();currentslide(3)" class="hover-shadow cursor"> </div> <div class="column"> <img src="img_lights.jpg" style="width:100%" onclick="openmodal();currentslide(4)" class="hover-shadow cursor"> </div> </div> <div id="mymodal" class="modal"> <span class="close cursor" onclick="closemodal()">×</span> <div class="modal-content"> <div class="myslides"> <div class="numbertext">1 / 4</div> <img src="img_nature_wide.jpg" style="width:100%"> </div> <div class="myslides"> <div class="numbertext">2 / 4</div> <img src="img_fjords_wide.jpg" style="width:100%"> </div> <div class="myslides"> <div class="numbertext">3 / 4</div> <img src="img_mountains_wide.jpg" style="width:100%"> </div> <div class="myslides"> <div class="numbertext">4 / 4</div> <img src="img_lights_wide.jpg" style="width:100%"> </div> <a class="prev" onclick="plusslides(-1)">❮</a> <a class="next" onclick="plusslides(1)">❯</a> <div class="caption-container"> <p id="caption"></p> </div> <div class="column"> <img class="demo cursor" src="img_nature_wide.jpg" style="width:100%" onclick="currentslide(1)" alt="nature , sunrise"> </div> <div class="column"> <img class="demo cursor" src="img_fjords_wide.jpg" style="width:100%" onclick="currentslide(2)" alt="trolltunga, norway"> </div> <div class="column"> <img class="demo cursor" src="img_mountains_wide.jpg" style="width:100%" onclick="currentslide(3)" alt="mountains , fjords"> </div> <div class="column"> <img class="demo cursor" src="img_lights_wide.jpg" style="width:100%" onclick="currentslide(4)" alt="northern lights"> </div> </div> </div> <script> function openmodal() { document.getelementbyid('mymodal').style.display = "block"; } function closemodal() { document.getelementbyid('mymodal').style.display = "none"; } var slideindex = 1; showslides(slideindex); function plusslides(n) { showslides(slideindex += n); } function currentslide(n) { showslides(slideindex = n); } function showslides(n) { var i; var slides = document.getelementsbyclassname("myslides"); var dots = document.getelementsbyclassname("demo"); var captiontext = document.getelementbyid("caption"); if (n > slides.length) {slideindex = 1} if (n < 1) {slideindex = slides.length} (i = 0; < slides.length; i++) { slides[i].style.display = "none"; } (i = 0; < dots.length; i++) { dots[i].classname = dots[i].classname.replace(" active", ""); } slides[slideindex-1].style.display = "block"; dots[slideindex-1].classname += " active"; captiontext.innerhtml = dots[slideindex-1].alt; } </script> </body> </html>
try if u using bootstrap
html
<div class="container"> <div class="row"> <div class="column"> <img src="https://fakeimg.pl/200x200/" onclick="openmodal();currentslide(1)" class="hover-shadow"> </div> <div class="column"> <img src="https://fakeimg.pl/200x200/" onclick="openmodal();currentslide(2)" class="hover-shadow"> </div> <div class="column"> <img src="https://fakeimg.pl/200x200/" onclick="openmodal();currentslide(3)" class="hover-shadow"> </div> <div class="column"> <img src="https://fakeimg.pl/200x200/" onclick="openmodal();currentslide(4)" class="hover-shadow"> </div> </div> <div id="mymodal" class="modal"> <span class="close cursor" onclick="closemodal()">×</span> <div class="modal-content"> <div class="myslides"> <div class="numbertext">1 / 4</div> <img src="https://fakeimg.pl/2500x500/" style="width:100%"> </div> <div class="myslides"> <div class="numbertext">2 / 4</div> <img src="https://fakeimg.pl/2500x500/" style="width:100%"> </div> <div class="myslides"> <div class="numbertext">3 / 4</div> <img src="https://fakeimg.pl/2500x500/" style="width:100%"> </div> <div class="myslides"> <div class="numbertext">4 / 4</div> <img src="https://fakeimg.pl/2500x500/" style="width:100%"> </div> <a class="prev" onclick="plusslides(-1)">❮</a> <a class="next" onclick="plusslides(1)">❯</a> <div class="caption-container"> <p id="caption"></p> </div> <div class="column"> <img class="demo" src="https://fakeimg.pl/250x250/" onclick="currentslide(1)"> </div> <div class="column"> <img class="demo" src="https://fakeimg.pl/250x250/" onclick="currentslide(2)"> </div> <div class="column"> <img class="demo" src="https://fakeimg.pl/250x250/" onclick="currentslide(3)"> </div> <div class="column"> <img class="demo" src="https://fakeimg.pl/250x250/" onclick="currentslide(4)"> </div> </div> </div> </div>
css
.row > .column { padding: 0 8px; } .row:after { content: ""; display: table; clear: both; } .column { float: left; width: 25%; } /* modal (background) */ .modal { display: none; position: fixed; z-index: 1; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: black; } /* modal content */ .modal-content { position: relative; background-color: #fefefe; margin: auto; padding: 0; width: 90%; max-width: 1200px; } /* close button */ .close { color: white; position: absolute; top: 10px; right: 25px; font-size: 35px; font-weight: bold; } .close:hover, .close:focus { color: #999; text-decoration: none; cursor: pointer; } .myslides { display: none; } /* next & previous buttons */ .prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; padding: 16px; margin-top: -50px; color: white; font-weight: bold; font-size: 20px; transition: 0.6s ease; border-radius: 0 3px 3px 0; user-select: none; -webkit-user-select: none; } /* position "next button" right */ .next { right: 0; border-radius: 3px 0 0 3px; } /* on hover, add black background color little bit see-through */ .prev:hover, .next:hover { background-color: rgba(0, 0, 0, 0.8); } /* number text (1/3 etc) */ .numbertext { color: #f2f2f2; font-size: 12px; padding: 8px 12px; position: absolute; top: 0; } .caption-container { text-align: center; background-color: black; padding: 2px 16px; color: white; } img.demo { opacity: 0.6; } .active, .demo:hover { opacity: 1; } img.hover-shadow { transition: 0.3s } .hover-shadow:hover { box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19) }
js
function openmodal() { document.getelementbyid('mymodal').style.display = "block"; } function closemodal() { document.getelementbyid('mymodal').style.display = "none"; } var slideindex = 1; showslides(slideindex); function plusslides(n) { showslides(slideindex += n); } function currentslide(n) { showslides(slideindex = n); } function showslides(n) { var i; var slides = document.getelementsbyclassname("myslides"); var dots = document.getelementsbyclassname("demo"); var captiontext = document.getelementbyid("caption"); if (n > slides.length) {slideindex = 1} if (n < 1) {slideindex = slides.length} (i = 0; < slides.length; i++) { slides[i].style.display = "none"; } (i = 0; < dots.length; i++) { dots[i].classname = dots[i].classname.replace(" active", ""); } slides[slideindex-1].style.display = "block"; dots[slideindex-1].classname += " active"; captiontext.innerhtml = dots[slideindex-1].alt; }
Comments
Post a Comment