我剛剛創(chuàng)建了一個lightbox模型來顯示圖像。單擊圖像時,會出現(xiàn)一個帶有導(dǎo)航按鈕的模式窗口,用于在圖像之間切換。但是,目前通過點(diǎn)擊空白區(qū)域來關(guān)閉模態(tài)窗口存在一個問題。似乎元素的z索引值導(dǎo)致了沖突,盡管我試圖給它們分配不同的值。我將非常感謝你的幫助來解決這個問題。謝謝你。
<style>
/* Mobile Styles */
@media (max-width: 767px){
.mySlides img {
width: 100%;
}
}
/* Desktop Styles */
@media (min-width: 768px) {
.mySlides img {
width: 70%;
}
}
.mySlides img{margin: 0 auto;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-width: 100%;
max-height: 100%;}
.mySlides{height: 100vh;}
.background-image {
position: fixed;
z-index: auto;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-attachment: fixed;
cursor: pointer;
}
* {
box-sizing: border-box;
}
.row > .column {
padding: 0 8px;
}
.row:after {
content: "";
display: table;
clear: both;
}
.column {
float: left;
width: 25%;
}
/* The Modal (background) */
.modal {
display: none;
position: fixed;
z-index: auto;
padding-top: 10px;
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: 100%;
/* max-width: 1200px; */
}
/* The 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;
text-align: center; /* Center the image within the container */
}
.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;
z-index: auto;
}
/* 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);
}
img {
margin-bottom: -4px;
}
.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);
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 20px;
align-items: stretch;
justify-items: center;
padding: 20px;
}
.grid img {
box-shadow: 2px 2px 6px 0px rgba(0,0,0,0.3);
max-width: 100%;
}
</style>
<div class="parent-cont">
<main class="grid">
<img onclick="openModal();currentSlide(1)" src="https://only4gamers.net/wp-content/uploads/2023/05/New-2.png" alt="Sample photo">
<img onclick="openModal();currentSlide(2)" src="https://only4gamers.net/wp-content/uploads/2023/05/New-3.png" alt="Sample photo">
<img onclick="openModal();currentSlide(3)" src="https://only4gamers.net/wp-content/uploads/2023/05/New-13.png" alt="Sample photo">
<img onclick="openModal();currentSlide(4)" src="https://only4gamers.net/wp-content/uploads/2023/05/New-12.png" alt="Sample photo">
</main>
</div>
</div>
<div id="myModal" class="modal">
<img src="https://only4gamers.net/wp-content/uploads/2023/05/Transparent-1-pixel.png" class="background-image" onclick="closeModal()">
<div class="modal-content">
<div class="mySlides">
<img style="z-index: auto;" src="https://only4gamers.net/wp-content/uploads/2023/05/New-2.png">
</div>
<div class="mySlides">
<img style="z-index: auto;" src="https://only4gamers.net/wp-content/uploads/2023/05/New-3.png">
</div>
<div class="mySlides">
<img style="z-index: auto;" src="https://only4gamers.net/wp-content/uploads/2023/05/New-13.png">
</div>
<div class="mySlides">
<img style="z-index: auto;" src="https://only4gamers.net/wp-content/uploads/2023/05/New-12.png">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
</div>
https://codepen.io/Only4Gamers/pen/PoyXojW
帶有事件處理程序onclick = & quotcloseModal()& quot;位于modal上顯示的圖像之后(指包含在& ltdiv class = & quot模態(tài)內(nèi)容& quot& gt因?yàn)閮煞鶊D像的z索引都設(shè)置為自動。如果具有相同z-index值的兩個元素相互重疊,那么代碼中后面出現(xiàn)的元素將被放在最上面。
Fix:設(shè)置z-index:1;在背景圖片CSS類將修復(fù)這個問題。
雖然你所報告的問題將會通過上述修改得到解決,但是當(dāng)點(diǎn)擊顯示在模態(tài)界面上的圖像時,它也會導(dǎo)致模態(tài)界面關(guān)閉。發(fā)生這種情況的原因是,用戶會在不知情的情況下點(diǎn)擊附加了closeModal事件處理程序的透明圖像,并且該透明圖像位于整個模式內(nèi)容的頂部。
更好的設(shè)計是在div上附加closeModal事件處理程序,使用class = & quot模態(tài)內(nèi)容& quot并在單擊模式或?qū)Ш桨粹o上顯示的圖像時取消單擊事件。
進(jìn)行以下所有更改:
超文本標(biāo)記語言
<div id="parent-cont">
<main class="grid">
<img onclick="openModal();currentSlide(1)" src="http://only4gamers.net/wp-content/uploads/2023/05/New-2.png" alt="Sample photo">
<img onclick="openModal();currentSlide(2)" src="http://only4gamers.net/wp-content/uploads/2023/05/New-3.png" alt="Sample photo">
<img onclick="openModal();currentSlide(3)" src="http://only4gamers.net/wp-content/uploads/2023/05/New-13.png" alt="Sample photo">
<img onclick="openModal();currentSlide(4)" src="http://only4gamers.net/wp-content/uploads/2023/05/New-12.png" alt="Sample photo">
</main>
</div>
<div id="myModal" class="modal">
<div class="modal-content" onclick="closeModal()">
<div class="mySlides" onclick="cancelEvent(event)">
<img src="http://only4gamers.net/wp-content/uploads/2023/05/New-2.png">
</div>
<div class="mySlides" onclick="cancelEvent(event)">
<img src="http://only4gamers.net/wp-content/uploads/2023/05/New-3.png">
</div>
<div class="mySlides" onclick="cancelEvent(event)">
<img src="http://only4gamers.net/wp-content/uploads/2023/05/New-13.png">
</div>
<div class="mySlides" onclick="cancelEvent(event)">
<img src="http://only4gamers.net/wp-content/uploads/2023/05/New-12.png">
</div>
<a class="prev" onclick="plusSlides(-1, event)">❮</a>
<a class="next" onclick="plusSlides(1, event)">❯</a>
</div>
</div>
半鑄鋼?鋼性鑄鐵(Cast Semi-Steel)
/* Mobile Styles */
@media (max-width: 767px){
.mySlides img {
width: 100%;
}
}
/* Desktop Styles */
@media (min-width: 768px) {
.mySlides img {
width: 70%;
}
}
.mySlides img{margin: 0 auto;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-width: 100%;
max-height: 100%;}
.mySlides{
height: 100vh;
width: 70%;
margin: auto;
}
* {
box-sizing: border-box;
}
.row > .column {
padding: 0 8px;
}
.row:after {
content: "";
display: table;
clear: both;
}
.column {
float: left;
width: 25%;
}
/* The Modal (background) */
.modal {
display: none;
position: fixed;
z-index: auto;
padding-top: 10px;
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: 100%;
/* max-width: 1200px; */
}
/* The 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;
text-align: center; /* Center the image within the container */
}
.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;
z-index: auto;
}
/* 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);
}
img {
margin-bottom: -4px;
}
.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);
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 20px;
align-items: stretch;
justify-items: center;
padding: 20px;
}
.grid img {
box-shadow: 2px 2px 6px 0px rgba(0,0,0,0.3);
max-width: 100%;
}
射流研究…
function openModal() {
document.getElementById("myModal").style.display = "block";
document.getElementById("parent-cont").style.display = "none";
}
function closeModal() {
document.getElementById("myModal").style.display = "none";
document.getElementById("parent-cont").style.display = "block";
}
function cancelEvent(event) {
event.stopPropagation();
}
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n, event) {
cancelEvent(event);
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
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";
}