2017-01-23 4 views
0

In meinem HTML habe ich mehrere Artikel. Jeder Artikel enthält ein Bild, und wenn Sie auf eines klicken, wird ein modales Fenster angezeigt. Meine Frage ist also, wie kann ich das Bild aus dem Artikel im modalen Fenster anzeigen lassen?Anzeige Bild im modalen Fenster von HTML-Seite

Wenn Sie meinen Code Check überprüfen diese:

var article = document.querySelectorAll("article"); 
 
    var modal = document.querySelector(".modal"); 
 
    var cross = document.querySelector(".cross"); 
 
    var contentModal = document.querySelector(".modal .contentModal img"); 
 
    var image = document.querySelectorAll("article img"); 
 

 
    var funcModal = function (value) { 
 
     article[value].addEventListener("click", function() { 
 
      modal.style.visibility = "visible"; 
 
      modal.style.opacity = "1"; 
 
      contentModal.style.src = image; 
 
     }) 
 
     cross.addEventListener("click", function() { 
 
      modal.style.visibility = "hidden"; 
 
      modal.style.opacity = "0"; 
 
      modal.style.transition = "opacity 1s"; 
 
     }) 
 
    } 
 

 
    for(var i = 0; i < article.length; i++) { 
 
     funcModal(i); 
 
    }
/*//////////////////////////////////////////////////////////////////// 
 

 

 

 
           Commons 
 

 

 

 
////////////////////////////////////////////////////////////////////*/ 
 

 
* { 
 
    margin: 0; 
 
    padding: 0; 
 
    border: 0; 
 
} 
 
*,*:before,*:after { 
 
    box-sizing: border-box; 
 
} 
 
body { 
 
    font-size: 62.5%; 
 
    font-family: 'Alegreya Sans SC', sans-serif; 
 
} 
 
.cf:before, 
 
.cf:after { 
 
    content: " "; /* 1 */ 
 
    display: table; /* 2 */ 
 
} 
 

 
.cf:after { 
 
    clear: both; 
 
} 
 

 
/** 
 
* For IE 6/7 only 
 
* Include this rule to trigger hasLayout and contain floats. 
 
*/ 
 
.cf { 
 
    *zoom: 1; 
 
} 
 
/*//////////////////////////////////////////////////////////////////// 
 

 

 

 
           Header 
 

 

 

 
////////////////////////////////////////////////////////////////////*/ 
 
header ul { 
 
    display: flex; 
 
    justify-content: center; 
 
} 
 
header li { 
 
    display: inline-flex; 
 
    margin-right: 20px; 
 
    margin-top: 20px; 
 
} 
 
header a { 
 
    color: #34495e; 
 
    text-decoration: none; 
 
    font-size: 2.5em; 
 
} 
 
header a:hover { 
 
    color: #000; 
 
} 
 
/*//////////////////////////////////////////////////////////////////// 
 

 

 

 
          Section > article 
 

 

 

 
////////////////////////////////////////////////////////////////////*/ 
 
article { 
 
    height: 200px; 
 
    background-color: #eee; 
 
    margin-top: 3.5%; 
 
    margin-left: 1%; 
 
    margin-right: 1%; 
 
    cursor: pointer; 
 
    border-radius: 5px; 
 
} 
 
article img { 
 
    width: 100%; 
 
    border-radius: 10px; 
 
} 
 
.col-20 { 
 
    float: left; 
 
    width: 18%; 
 
} 
 

 
/*//////////////////////////////////////////////////////////////////// 
 

 

 

 
          Fênetre modale 
 

 

 

 
////////////////////////////////////////////////////////////////////*/ 
 
.modal { 
 
    visibility: hidden; 
 
    opacity: 0; 
 
    transition: opacity 1s; 
 
    background-color: rgba(0,0,0,0.3); 
 
    margin: auto; 
 
    position: absolute; 
 
    left: 0; 
 
    top: 0; 
 
    bottom: 0; 
 
    right: 0; 
 
} 
 
.modal i { 
 
    position: absolute; 
 
    right: 10px; 
 
    top: 10px; 
 
    font-size: 40px; 
 
    color: #34495e; 
 
    cursor: pointer; 
 
} 
 
.contentModal { 
 
    margin: auto; 
 
    width: 50%; 
 
    height: 50%; 
 
    border-radius: 10px; 
 
    background-color: #fff; 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    bottom: 0; 
 
    right: 0; 
 
} 
 
.contentModal img { 
 
    width: 100%; 
 
}
<section class="cf"> 
 
     <article class="col-20"> 
 
      <img src="img-content/france/1.jpg" alt=""> 
 
     </article> 
 
     <article class="col-20"> 
 
      <img src="img-content/france/1.jpg" alt=""> 
 
     </article> 
 
     <article class="col-20"> 
 
      <img src="img-content/france/1.jpg" alt=""> 
 
     </article> 
 
     <article class="col-20"> 
 
      <img src="img-content/france/1.jpg" alt=""> 
 
     </article> 
 
     <article class="col-20"> 
 
      <img src="img-content/france/1.jpg" alt=""> 
 
     </article> 
 
     <article class="col-20"> 
 
      <img src="img-content/france/1.jpg" alt=""> 
 
     </article> 
 
     <article class="col-20"> 
 
      <img src="img-content/france/1.jpg" alt=""> 
 
     </article> 
 
     <article class="col-20"> 
 
      <img src="img-content/france/1.jpg" alt=""> 
 
     </article> 
 
     <article class="col-20"> 
 
      <img src="img-content/france/1.jpg" alt=""> 
 
     </article> 
 
     <article class="col-20"></article> 
 
     <article class="col-20"></article> 
 
     <article class="col-20"></article> 
 
     <article class="col-20"></article> 
 
     <article class="col-20"></article> 
 
     <article class="col-20"></article> 
 
     <div class="modal"> 
 
      <i class="fa fa-times cross"></i> 
 
      <div class="contentModal"> 
 
       <img src="" alt=""> 
 
      </div> 
 
     </div> 
 
    </section>

+0

Ich glaube, Sie zu fragen, wie die Quelle des Bildes in der modal eingestellt das gleiche wie die in der Miniatur zu sein? Sie sollten einen Verweis auf das Bild innerhalb des Klickereignisses des Artikels erhalten und nicht außerhalb. –

+0

@MikeMcCaughan: Ja, ich suche nach einer Technik, so dass das Bild meines Thumbnails für mein modales Fenster dasselbe ist. Ich wünsche dir hoffe das Problem zu verstehen – KolaCaine

Antwort

0

Gerade jetzt sind Sie die image Variable einmal einstellen, bevor irgendetwas angeklickt wird (und es zu einer Liste Einstellung von Bildern). Sie möchten diese Variable jedes Mal festlegen, wenn auf einen Artikel geklickt wird, auf das Bild des Artikels, auf den geklickt wurde.

var article = document.querySelectorAll("article"); 
 
    var modal = document.querySelector(".modal"); 
 
    var modalImage = modal.querySelector('img'); 
 
    var cross = document.querySelector(".cross"); 
 

 
    var funcModal = function(value) { 
 
     article[value].addEventListener("click", function() { 
 
     modal.style.visibility = "visible"; 
 
     modal.style.opacity = "1"; 
 
     // here, "this" will be a reference to the article clicked 
 
     var image = this.querySelector('img'); 
 
     modalImage.src = image.src; 
 
     }) 
 
     cross.addEventListener("click", function() { 
 
     modal.style.visibility = "hidden"; 
 
     modal.style.opacity = "0"; 
 
     modal.style.transition = "opacity 1s"; 
 
     }) 
 
    } 
 

 
    for (var i = 0; i < article.length; i++) { 
 
     funcModal(i); 
 
    }
/*//////////////////////////////////////////////////////////////////// 
 

 

 

 
           Commons 
 

 

 

 
////////////////////////////////////////////////////////////////////*/ 
 

 
* { 
 
    margin: 0; 
 
    padding: 0; 
 
    border: 0; 
 
} 
 
*, 
 
*:before, 
 
*:after { 
 
    box-sizing: border-box; 
 
} 
 
body { 
 
    font-size: 62.5%; 
 
    font-family: 'Alegreya Sans SC', sans-serif; 
 
} 
 
.cf:before, 
 
.cf:after { 
 
    content: " "; 
 
    /* 1 */ 
 
    display: table; 
 
    /* 2 */ 
 
} 
 
.cf:after { 
 
    clear: both; 
 
} 
 
/** 
 
* For IE 6/7 only 
 
* Include this rule to trigger hasLayout and contain floats. 
 
*/ 
 

 
.cf { 
 
    *zoom: 1; 
 
} 
 
/*//////////////////////////////////////////////////////////////////// 
 

 

 

 
           Header 
 

 

 

 
////////////////////////////////////////////////////////////////////*/ 
 

 
header ul { 
 
    display: flex; 
 
    justify-content: center; 
 
} 
 
header li { 
 
    display: inline-flex; 
 
    margin-right: 20px; 
 
    margin-top: 20px; 
 
} 
 
header a { 
 
    color: #34495e; 
 
    text-decoration: none; 
 
    font-size: 2.5em; 
 
} 
 
header a:hover { 
 
    color: #000; 
 
} 
 
/*//////////////////////////////////////////////////////////////////// 
 

 

 

 
          Section > article 
 

 

 

 
////////////////////////////////////////////////////////////////////*/ 
 

 
article { 
 
    height: 200px; 
 
    background-color: #eee; 
 
    margin-top: 3.5%; 
 
    margin-left: 1%; 
 
    margin-right: 1%; 
 
    cursor: pointer; 
 
    border-radius: 5px; 
 
} 
 
article img { 
 
    width: 100%; 
 
    border-radius: 10px; 
 
} 
 
.col-20 { 
 
    float: left; 
 
    width: 18%; 
 
} 
 
/*//////////////////////////////////////////////////////////////////// 
 

 

 

 
          Fênetre modale 
 

 

 

 
////////////////////////////////////////////////////////////////////*/ 
 

 
.modal { 
 
    visibility: hidden; 
 
    opacity: 0; 
 
    transition: opacity 1s; 
 
    background-color: rgba(0, 0, 0, 0.3); 
 
    margin: auto; 
 
    position: absolute; 
 
    left: 0; 
 
    top: 0; 
 
    bottom: 0; 
 
    right: 0; 
 
} 
 
.modal i { 
 
    position: absolute; 
 
    right: 10px; 
 
    top: 10px; 
 
    font-size: 40px; 
 
    color: #34495e; 
 
    cursor: pointer; 
 
} 
 
.contentModal { 
 
    margin: auto; 
 
    width: 50%; 
 
    height: 50%; 
 
    border-radius: 10px; 
 
    background-color: #fff; 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    bottom: 0; 
 
    right: 0; 
 
} 
 
.contentModal img { 
 
    width: 100%; 
 
}
<section class="cf"> 
 
    <article class="col-20"> 
 
    <img src="img-content/france/1.jpg" alt=""> 
 
    </article> 
 
    <article class="col-20"> 
 
    <img src="img-content/france/1.jpg" alt=""> 
 
    </article> 
 
    <article class="col-20"> 
 
    <img src="img-content/france/1.jpg" alt=""> 
 
    </article> 
 
    <article class="col-20"> 
 
    <img src="img-content/france/1.jpg" alt=""> 
 
    </article> 
 
    <article class="col-20"> 
 
    <img src="img-content/france/1.jpg" alt=""> 
 
    </article> 
 
    <article class="col-20"> 
 
    <img src="img-content/france/1.jpg" alt=""> 
 
    </article> 
 
    <article class="col-20"> 
 
    <img src="img-content/france/1.jpg" alt=""> 
 
    </article> 
 
    <article class="col-20"> 
 
    <img src="img-content/france/1.jpg" alt=""> 
 
    </article> 
 
    <article class="col-20"> 
 
    <img src="img-content/france/1.jpg" alt=""> 
 
    </article> 
 
    <article class="col-20"></article> 
 
    <article class="col-20"></article> 
 
    <article class="col-20"></article> 
 
    <article class="col-20"></article> 
 
    <article class="col-20"></article> 
 
    <article class="col-20"></article> 
 
    <div class="modal"> 
 
    <i class="fa fa-times cross"></i> 
 
    <div class="contentModal"> 
 
     <img src="" alt=""> 
 
    </div> 
 
    </div> 
 
</section>

+0

Hum, richtig verstehe ich. Danke, dass du dich für einen Teil deiner Zeit entscheidest! – KolaCaine

Verwandte Themen