Открытие модального окна по клику на изображение (при использовании нескольких изображений с использованием одного модалCSS

Разбираемся в CSS
Ответить
Anonymous
 Открытие модального окна по клику на изображение (при использовании нескольких изображений с использованием одного модал

Сообщение Anonymous »

Я пытаюсь создать страницу с фотографиями для своей веб-страницы, и при щелчке по изображению я хочу открыть модальное окно для просмотра изображения в полноэкранном режиме (более или менее). Я нашел это руководство по использованию модального окна для увеличения изображения — https://www.w3schools.com/howto/howto_c ... images.asp

Однако я не уверен, что есть более простой способ увеличить изображение по клику, учитывая, что на странице несколько изображений и есть одно модальное окно.

Это единственный способ создать отдельный идентификатор для каждого изображения или в JS есть селектор для выбора нескольких изображений одновременно?
Вот JSfiddle — https://jsfiddle.net/definaly/89roukqd/2/

Код: Выделить всё

// 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 = $(".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  element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on  (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}

Код: Выделить всё

/* Style the Image Used to Trigger the Modal */
.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%;
}
}

Код: Выделить всё



[img]https://picsum.photos/300/300?random=1[/img]

[img]https://picsum.photos/300/300?random=2[/img]

[img]https://picsum.photos/300/300?random=3[/img]





×








Заранее спасибо

Подробнее здесь: https://stackoverflow.com/questions/567 ... ngle-modal
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «CSS»