generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathimage-modal.html
53 lines (44 loc) · 1.49 KB
/
image-modal.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<div id="myModal" class="modal">
<button class="modal-close" onclick="closeModal()">close</button>
<div class="modal-cont">
<img class="modal-pic" id="modalPic" onclick="closeModal()" style="max-width: 100%; max-height: 80vh; margin: auto;" alt="Modal-pic">
</div>
</div>
<script>
// Open the Modal
function openModal(imageIdOrElement) {
var src;
if (typeof imageIdOrElement === 'string') {
// If it's a string (image ID), get the source using the ID
src = document.getElementById(imageIdOrElement).src;
} else if (imageIdOrElement instanceof HTMLImageElement) {
// If <img>, get the source directly
src = imageIdOrElement.src;
}
if (src && src.includes("#")) {
src = src.substring(0, src.indexOf("#"));
}
document.getElementById("modalPic").src = src;
document.getElementById("myModal").style.display = "block";
document.getElementById("site-footer").style.display = "hidden";
}
// Close the Modal
function closeModal() {
document.getElementById("modalPic").src = "";
document.getElementById("myModal").style.display = "none";
document.getElementById("site-footer").style.display = "block";
}
</script>
<!-- To attach onclick attribute to all <img> -->
<script>
document.addEventListener("DOMContentLoaded", function () {
var imgTags = document.querySelectorAll("img");
imgTags.forEach(function (img) {
img.onclick = function () {
if(img.id !== "navbar-brand_logo") {
openModal(img);
}
};
});
});
</script>