0
在w3schools網站上,我找到了一個對話框/彈出窗口的教程。我想用更多的圖像來實現這一點。如何打開多個圖像的模態圖像
我的代碼:
<img onclick="openModal()" src="low/dn-64.jpg" data-highres="high/dn-64.jpg" width="300" height="400">
<!-- Modal -->
<div id="myModal" class="modal">
<!-- Close button -->
<span class="close">×</span>
<!-- Modal content -->
<img class="modal-content" id="modal-image">
</div>
<script type="text/javascript">
var modal = document.getElementById('myModal');
var img = document.getElementById('myImg');
var modalImg = document.getElementById('modal-image');
function openModal() {
modal.style.display = "block";
modalImg.src = this.getAttribute("data-highres");
}
/*img.onclick = function() {
modal.style.display = "block";
modalImg.src = this.getAttribute("data-highres");
}*/
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function() {
if(event.target == modal) {
modal.style.display = "none";
}
}
</script>
CSS:
#myImg {
cursor: zoom-in;
transition: 0.3s;
}
#myImg:hover {
opacity: 0.7;
}
.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 60px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.8);
}
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 50px;
font-weight: bold;
transition: 0.3s;
}
.close:hover,
.close:focus {
color: #bbbbbb;
text-decoration: none;
cursor: pointer;
}
代碼正在爲一個圖像。所以我添加了一個函數openModal()並取消註釋了以前代碼的某些部分。但是現在,即使對於一張圖像,它也不起作用,它會在沒有圖像的情況下打開模式。
感謝你的幫助
謝謝你的快速解決方案! –