簡體   English   中英

w3schools.com 的模態圖像關閉預覽不起作用

[英]Modal image close preview from w3schools.com not working

這是 w3 頁面https://www.w3schools.com/howto/howto_css_modal_images.asp的鏈接,所做的所有更改僅在 javascript 中。 請注意,w3 頁面中的那個不起作用,所以我不得不從這個頁面找到一個解決方案,發現這個https://jsfiddle.net/snowMonkey/f1zav0ge/並且我只從這里獲取了 javascript 代碼,但兩者仍然不起作用

這是 html:

<html>
    <head>
        <title>Gallery | safsafa</title>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="gallery.css">
        <link rel = "icon" href =  "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRUXLH90QAaWzieHLTW9BxxNvSKIEhAzRzJ1g&usqp=CAU" type = "image/x-icon"> 
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
        <link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
    </head>
    <body>
        <header>
            
                <h1><span><i class="fa fa-trophy" aria-hidden="true"></i> fsasaf</span></h1>
                    <ul class="nav_links">
                        <li><a href="tesAbc.html" >Home</a></li><li><a href="gallery.html">Gallery</a></li><li><a href="#">Showcase</a></li><li><a href="#">About</a></li><li class="nav-seperator">|</li>
                    </ul>
        </header>
        <main>
            <div class="container">
            
            <h2>//CLICK THESE IMAGES</h2><br>
            <img class="myImages" id="myImg" src="image/racoon.jpg" alt="A Racoon" style="width:100%;max-width:300px">
            <img class="myImages" id="myImg" src="image/hamster.jpg" alt="A Hamster" style="width:100%;max-width:300px">
            <img class="myImages"id="myImg" src="image/hedgehog.jpg" alt="A Hedgehog" style="width:100%;max-width:300px">
            <img class="myImages" id="myImg" src="image/artic_fox.jpg" alt="An Artic Fox" style="width:100%;max-width:300px">
            <!-- The Modal -->
            <div id="myModal" class="modal">
            <span class="close">&times;</span>
            <img class="modal-content" id="img01">
            <div id="caption"></div>
            </div>
            
            </div>
        </main>
        
        <script>
            window.addEventListener('scroll',function(){
                var header = document.querySelector("header");
                header.classList.toggle('sticky',window.scrollY > 0)
            })
        </script>
        <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
        <script src="aos.js"></script>
          <script src="https://unpkg.com/aos@next/dist/aos.js"></script>
  <script>
    AOS.init();
  </script>
  <script>
// create references to the modal...
var modal = document.getElementById('myModal');
// to all images -- note I'm using a class!
var images = document.getElementsByClassName('myImages');
// the image in the modal
var modalImg = document.getElementById("img01");
// and the caption in the modal
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) {
    console.log(evt);
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
  }
}

var span = document.getElementsByClassName("close")[0];

span.onclick = function() {
  modal.style.display = "none";
}
</script>

    </body>
</html>

這是 css:


@import url('https://fonts.googleapis.com/css2?family=Poppins&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Heebo:wght@500&display=swap');

*{
    margin: 0;
    padding: 0; 
}

body{
    background-color: #9dc1fc;
}
header{

    position: fixed;
    overflow: hidden;
    top: 0;
    width: 100%;
    background-color:;
    transition: 0.6s;
    z-index:10000000;
}
header span{
    font-family:Poppins, sans-serif;
    cursor: pointer;
    font-size: 125%;
    font-weight: 900;
    padding-left: 70px;
}
header .nav-seperator{
    color: black;
    cursor: pointer;
}


header span{
    letter-spacing:-2px;
    font-size: 115%;
    color: blackc;
}
header ul{
    font-family:Heebo, sans-serif;
    font-weight: 6200;
    font-size: 100%;
    list-style-type: none;
    padding-right: 100px;

}
header li{
    display: inline-block;
    
}
header li a{
    margin-right:30px;
    text-decoration: none;
    color: black;
    transition: all 0.3s ease 0s;
}
header li a:hover{
    color: #fe6e00;
}

header{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
}
header.sticky{
    background-color:rgba(0,0,0,0.5)
}
header.sticky ul li a{
    color: white;
}
header.sticky span{
    color: white;
}
header.sticky .nav-seperator{
    color: white;
}
/* MAIN CONTAINER*/
main .container{
    margin-top: 100px;
    margin-left: 200px;
    margin-right: 200px;
    text-align:center;
    text-transform:uppercase;
    color:black;
    font-family: Poppins;
    font-size: 2rem;
    font-weight: 900;
}
#myImg {
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
}

#myImg:hover {opacity: 0.7;}

/* 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: 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 as the 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 the Modal */
.modal-content, #caption {
  animation-name: zoom;
  animation-duration: 0.6s;
}

@keyframes zoom {
  from {transform:scale(0)}
  to {transform:scale(1)}
}

/* The 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 only screen and (max-width: 700px){
  .modal-content {
    width: 100%;
  }
}

目前,模態 window 上的關閉按鈕仍位於導航下方,因此無法單擊。 為了能夠關閉模態,有必要在 CSS 中進行這兩項更改:

在樣式header設置z-index: 10;

樣式.modal設置z-index: 11;

他所做的更改是,當模態 window 打開時,它將位於導航上方,並且可以單擊“關閉”按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM