Intendevi una cosa del genere ?
codice:
<!DOCTYPE html>
<html lang="it-IT">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html,
body {
height: 100vh;
}
#container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
#container .image {
margin: 10px;
cursor: pointer;
}
.modal {
width: 100%;
height: 100%;
justify-content: center;
align-items: center;
position: absolute;
display: none;
background-color: rgba(0, 0, 0, 0.5);
border: 1px solid red;
padding: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="container">
<img class="image" src="./images/01.jpg" alt="img1">
<img class="image" src="./images/02.jpg" alt="img2">
<img class="image" src="./images/03.jpg" alt="img3">
<div class="modal">modal-01</div>
<div class="modal">modal-02</div>
<div class="modal">modal-03</div>
</div>
<script>
const images = document.getElementsByClassName('image')
const modals = document.getElementsByClassName('modal')
let tmpModalIdx = null
Array.from(images).forEach((e, idx) => {
function openModal() {
tmpModalIdx === null
? tmpModalIdx = idx
: Array.from(modals)[tmpModalIdx].style.display = 'none'
tmpModalIdx = idx
currentModal = Array.from(modals)[idx]
Array.from(modals)[idx].style.display = 'flex'
}
e.addEventListener('click', openModal)
})
Array.from(modals).forEach(e => {
function closeModal(e) {
e.target.style.display = 'none'
}
e.addEventListener('click', closeModal)
})
</script>
</body>
</html>