幻灯片+JS模态框:实现点击图片或按钮打开当前幻灯片对应模态框
幻灯片+JS模态框:实现点击图片或按钮打开当前幻灯片对应模态框
我懂你的问题啦!现在你的幻灯片点击图片能正常打开对应模态框,但那个独立按钮还只会弹出空的模态框对吧?其实完全不用动原来的幻灯片循环逻辑,只给按钮的点击事件加几行代码,让它能“跟上”当前显示的幻灯片就行~
核心修改思路
按钮点击时,我们已经有了slideIndex这个变量记录当前是第几张幻灯片,只需要根据这个索引找到对应的图片元素,把它的地址和描述传给模态框就好,不会干扰原来的图片点击逻辑。
修改后的完整代码
HTML(无需修改,直接复用你的代码)
<body> <h2 style="text-align:center">Slideshow Gallery</h2> <div class="container"> <div class="mySlides"> <div class="numbertext">1 / 3</div> <img class="myImages" src="https://www.w3schools.com/howto/img_woods_wide.jpg" style="width:100%"> </div> <div class="mySlides"> <div class="numbertext">2 / 3</div> <img class="myImages" src="https://www.w3schools.com/howto/img_5terre_wide.jpg" style="width:100%"> </div> <div class="mySlides"> <div class="numbertext">3 / 3</div> <img class="myImages" src="https://www.w3schools.com/howto/img_mountains_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="slide_caption"></p> </div> <div class="row"> <div class="column"> <img class="demo cursor" src="https://www.w3schools.com/howto/img_woods_wide.jpg" style="width:100%" onclick="currentSlide(1)" alt="The Woods"> </div> <div class="column"> <img class="demo cursor" src="https://www.w3schools.com/howto/img_5terre_wide.jpg" style="width:100%" onclick="currentSlide(2)" alt="Cinque Terre"> </div> <div class="column"> <img class="demo cursor" src="https://www.w3schools.com/howto/img_mountains_wide.jpg" style="width:100%" onclick="currentSlide(3)" alt="Mountains and fjords"> </div> </div> </div> <div> <button id="myBtn">Open Modal</button> </div> <!-- The Modal --> <div id="myModal" class="modal"> <span class="close">×</span> <img id="img01" class="modal-content"> <div id="caption"></div> </div> </body>
CSS(无需修改,直接复用你的样式)
/* Position the image container (needed to position the left and right arrows) */ .container { position: relative; } /* Hide the images by default */ .mySlides { display: none; } /* Add a pointer when hovering over the thumbnail images */ .cursor { cursor: pointer; } /* Next & previous buttons */ .prev, .next { cursor: pointer; position: absolute; top: 40%; width: auto; padding: 16px; margin-top: -50px; color: white; font-weight: bold; font-size: 20px; border-radius: 0 3px 3px 0; user-select: none; -webkit-user-select: none; } /* Position the "next button" to the right */ .next { right: 0; border-radius: 3px 0 0 3px; } /* On hover, add a black background color with a 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; } /* Container for image text */ .caption-container { text-align: center; background-color: #222; padding: 2px 16px; color: white; } .row:after { content: ""; display: table; clear: both; } /* columns side by side */ .column { float: left; width: 16.66%; } /* Add a transparency effect for thumnbail images */ .demo { opacity: 0.6; } .active, .demo:hover { opacity: 1; } /********** MODAL **********/ /* 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: rgba(219, 219, 219, 0.9); } /* Modal Content (image) */ .modal-content { margin: auto; display: block; width: 40%; max-width: 40%; } /* Caption of Modal Image */ #caption { margin: auto; display: block; width: 80%; max-width: 700px; text-align: center; color: #000; padding: 10px 0; height: 150px; } /* Add Animation */ .modal-content, #caption { -webkit-animation-name: zoom; -webkit-animation-duration: 0.6s; animation-name: zoom; animation-duration: 0.6s; } @-webkit-keyframes zoom { from { -webkit-transform: scale(0) } to { -webkit-transform: scale(1) } } @keyframes zoom { from { transform: scale(0) } to { transform: scale(1) } } /* The Close Button */ .close { position: fixed; /* prev absolute */ top: 25px; /* prev 15 */ right: 75px; /* prev 35 */ color: #646363; 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%; } }
JavaScript(重点修改按钮点击事件,已标注)
let slideIndex = 1; showSlides(slideIndex); function plusSlides(n) { showSlides(slideIndex += n); } function currentSlide(n) { showSlides(slideIndex = n); } function showSlides(n) { let i; let slides = document.getElementsByClassName("mySlides"); let dots = document.getElementsByClassName("demo"); let captionText = document.getElementById("slide_caption"); if (n > slides.length) { slideIndex = 1 } if (n < 1) { slideIndex = slides.length } for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < 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; } // Get the modal var modal = document.getElementById("myModal"); var btn = document.getElementById("myBtn"); // Get the button that opens the modal // Create reference to the modal for all images using a class! var images = document.getElementsByClassName('myImages'); // Get each image and insert it inside the modal - use its "alt" text as a caption var modalImg = document.getElementById("img01"); var captionText = document.getElementById("caption"); // Go through all of the images with our custom class for (var i = 0; i < images.length; i++) { var img = images[i]; // and attach our click listener for this image. img.onclick = function(evt) { // click on <img> to run function modal.style.display = "block"; // function will display modal using class modalImg.src = this.src; // var modalImg gets <img> src captionText.innerHTML = this.alt; // var captionText gets <img> alt } } // 👇👇👇 这里是修改的核心部分 👇👇👇 // When the user clicks the button, open the modal with current slide image btn.onclick = function(evt) { // 根据当前slideIndex找到对应的图片(slideIndex从1开始,数组索引要-1) let currentImg = document.querySelectorAll('.myImages')[slideIndex - 1]; // 给模态框设置图片和标题 modalImg.src = currentImg.src; captionText.innerHTML = currentImg.alt; // 显示模态框 modal.style.display = "block"; } // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; // When the user clicks on <span> (x), close the modal span.onclick = function() { modal.style.display = "none"; } // When the user clicks anywhere outside of the modal, close it window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } }
为什么这样改不会干扰原有逻辑?
- 我们只是复用了已经存在的
slideIndex变量,没有修改幻灯片切换、图片点击的核心代码 - 按钮事件只是新增了“获取当前图片→赋值给模态框→显示模态框”的流程,和图片点击的逻辑完全对齐
这样修改后,不管你切换到哪张幻灯片,点击按钮都会弹出对应图片的模态框啦!
备注:内容来源于stack exchange,提问作者kent
相关产品推荐
相关产品推荐

