如何实现按下Escape键关闭模态弹窗及隐藏首图上一页按钮
功能实现修改方案
核心修改点
- 新增键盘事件监听实现ESC键关闭模态框
- 调整
showSlides函数逻辑,实现首张图片对应弹窗自动隐藏上一页按钮 - 修复原有代码中标题赋值异常、标签嵌套不规范、重复ID导致的运行报错问题
完整JavaScript代码
function openModal() { document.getElementById("myModal").style.display = "block"; } function closeModal() { document.getElementById("myModal").style.display = "none"; } // 新增ESC键关闭模态框逻辑 document.addEventListener('keydown', function(e) { const modal = document.getElementById("myModal"); // 仅当模态框处于显示状态时响应ESC按键 if (e.key === 'Escape' && modal.style.display === 'block') { closeModal(); } }) 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 prevBtn = document.querySelector('.prev'); var captionText = document.getElementById("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"; } slides[slideIndex - 1].style.display = "block"; // 新增首屏隐藏上一页按钮逻辑 prevBtn.style.display = slideIndex === 1 ? 'none' : 'block'; // 修复标题赋值异常问题 const currentImg = slides[slideIndex - 1].querySelector('img'); captionText.innerHTML = currentImg.alt || slides[slideIndex - 1].querySelector('p').innerText; }
完整CSS代码
.modal { width: 58%; height: 100%; top: 0; position: fixed; display: none; background-color: rgba(22, 22, 22, 0.5); margin-left: 300px; max-width: 779px; min-width: 779px; } .modal-content { margin: auto; display: block; width: 80%; max-width: 700px; } .mySlides { display: none; } .close { position: relative; color: #f1f1f1; font-size: 40px; font-weight: bold; transition: 0.3s; left: 584px; top: 90px; } .close:hover, .close:focus { color: #999; text-decoration: none; cursor: pointer; } .cursor { cursor: pointer; } .prev { cursor: pointer; position: relative; top: -149px; 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; left: -10%; } .next { cursor: pointer; position: relative; top: -149px; 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; left: 600px; }
完整HTML代码
<!DOCTYPE html> <html> <head> <title>Calcutta Public School > Photogallery</title> </head> <body> <div class="row"> <div class="column"> <p align="center"><img src="https://source.unsplash.com/user/erondu/1600x900" alt="Annual function" width="250" height="164" onclick="openModal();currentSlide(1)" class="hover-shadow cursor"></p> </div> <div class="column"> <p align="center"><img src="https://source.unsplash.com/collection/190727/1600x900" alt="Annual function" width="250" height="164" onclick="openModal();currentSlide(2)" class="hover-shadow cursor"></p> </div> </div> <div id="myModal" class="modal"> <div class="modal-content"> <span class="close cursor" onclick="closeModal()">×</span> <div class="mySlides"> <img src="https://source.unsplash.com/user/erondu/1600x900" style="width: 98%;position: relative;left: 10px;top: 109px;"> <p id="caption" style="padding-bottom: 7px;font-size: 17px;">Annual function</p> </div> <div class="mySlides"> <img src="https://source.unsplash.com/collection/190727/1600x900" style="width: 98%;position: relative;left: 10px;top: 109px;"> <p style="padding-bottom: 7px;font-size: 17px;">Annual function</p> </div> <a class="prev" id="prev1" onclick="plusSlides(-1)">❮</a> <a class="next" onclick="plusSlides(1)">❯</a> </div> </div> </body> </html>
内容的提问来源于stack exchange,提问作者Souvik
相关产品推荐
相关产品推荐

