单击图像链接时,模态不会弹出

时间:2018-03-16 02:51:48

标签: html css hyperlink modal-dialog

我试图通过点击图片链接弹出一个模态,但由于某种原因它不起作用。 这是我的HTML代码:

 <div id="imagesMain">
         <div class="imagebox">
            <img src="c://images/summer.jpg">
             <div class="caption">Estate in Giappone<h1>Tradizione e fascino 
 del Giappone rurale</h1></div>             
         </div>
         <div class="imagebox">
            <img src="c://images/fall.jpg">
             <div class="caption">Autunno in Giappone<h1>Foglie rosse e 
    giardini incantati </h1></div>             
         </div>
         <div class="imagebox">
            <img src="c://images/winter.jpg">
             <div class="caption">Inverno in Giappone<h1>La neve ed il 
 tepore degli onsen </h1></div>             
         </div>
         <div class="imagebox">
             <a href="#openmodal"><img src="c://images/spring.jpg"></a>
             <div class="caption">Primavera in Giappone<h1>Seguendo il 
 sakura tra templi e castelli</h1></div>             
         </div>

            <div id="openmodal" class="modaldialog">
    <div>
<a href="#close" title="Chiudi" class="close">X</a>
<h2>Sakura nel Tohoku</h2>
<table id="customers">
    <tr>
    <th>Giorno</th>
        <th>Percorso</th>
        <th>Pernottamento</th>
    </tr>
    <tr>
    <td>Giorno 1</td>
        <td>TOKYO/AOMORI/HINOSAKI</td>
        <td>Art Hotel Hirosaki City</td>
    </tr>
    <tr>
    <td>6:00</td>
        <td style="font-size:12px; text-align: justify; width: 
60%;">text</td>
        <td></td>
    </tr>
     <tr>
    <td>Giorno 2</td>
        <td>HIROSAKI/KAKUNODATE</td>
        <td>Hotel Folkloro</td>
    </tr>
    <tr>
    <td>9:00</td>
        <td style="font-size:12px; text-align: justify; width: 
 60%;">text</td>
        <td></td>
    </tr>
     <tr>
    <td>Giorno 2</td>
        <td>KAKUNODATE/MORIOKA/TOKYO</td>
        <td>-</td>
    </tr>
    <tr>
    <td>9:30</td>
        <td style="font-size:12px; text-align: justify; width: 
60%;">text</td>
        <td></td>
    </tr>
    </table>
     </div>
</div>
</div>   

这是CSS:

.modaldialog {
position: fixed;
font-family: Arial, Helvetica, sans-serif;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0,0,0,0.8);
z-index: 99999;
opacity:0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}

.modalDialog:target {
opacity:1;
pointer-events: auto;
}

.modalDialog > div {
width: 650px;
position: relative;
margin: 1% auto;
padding: 5px 20px 13px 20px;
border-radius: 10px;
background: #fff;
background: -moz-linear-gradient(#fff, #999);
background: -webkit-linear-gradient(#fff, #999);
background: -o-linear-gradient(#fff, #999);
}

.close {
background: #606061;
color: #FFFFFF;
line-height: 25px;
position: absolute;
right: -12px;
text-align: center;
top: -10px;
width: 24px;
text-decoration: none;
font-weight: bold;
-webkit-border-radius: 12px;
-moz-border-radius: 12px;
border-radius: 12px;
-moz-box-shadow: 1px 1px 3px #000;
-webkit-box-shadow: 1px 1px 3px #000;
box-shadow: 1px 1px 3px #000;
}

.close:hover { background: #00d9ff; }

#customers {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
font-size: 14px;
border-collapse: collapse;
width: 100%;
}



#customers td, #customers th {
border: 1px solid #ddd;
padding: 8px;
}

#customers tr:nth-child(even){background-color: #f2f2f2;}

#customers tr:hover {background-color: #ddd;}

#customers th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #4CAF50;
color: white;
}

如果我独立运行代码(单独的页面,只有一个图像),它可以完美地运行。 有没有人可以帮我修复这个bug? 非常感谢你提前, 干杯 瓦尔特

1 个答案:

答案 0 :(得分:0)

试试此代码

  

但在此你必须使用javascript。

// Get the modal
var modal = document.getElementById('myModal');

// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
    modal.style.display = "block";
    modalImg.src = this.src;
    captionText.innerHTML = this.alt;
}

// 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";
}
body {font-family: Arial, Helvetica, sans-serif;}

#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 */
#caption {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
    text-align: center;
    color: #ccc;
    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: 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%;
    }
}
<img id="myImg" src="https://images.pexels.com/photos/34950/pexels-photo.jpg?w=1260&h=750&auto=compress&cs=tinysrgb" width="300" height="200">

<!-- The Modal -->
<div id="myModal" class="modal">
  <span class="close">&times;</span>
  <img class="modal-content" id="img01">
  <div id="caption"></div>
</div>

相关问题