Я пытаюсь создать несколько модальных изображений для своего веб-сайта, но я просто не могу настроить код, чтобы он работал. Буду очень признателен за любую помощь или предложения, так как я все еще учусь, и это уже не давало мне покоя:)
HTML:
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
Сценарий:
// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img");
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";
}