feat: image enlarge

This commit is contained in:
KazooTTT
2025-02-06 00:07:18 +08:00
parent d0a06cc104
commit 6d9c280a84
5 changed files with 96 additions and 9 deletions

View File

@ -0,0 +1,87 @@
---
import { Icon } from "astro-icon/components";
export interface Props {
className?: string;
dataPagefindBody?: boolean;
}
const { className = "", dataPagefindBody = true } = Astro.props;
---
<article class={`${className}`} data-pagefind-body={dataPagefindBody}>
<slot />
<div id="myModal" class="modal">
<span class="close">
<Icon aria-hidden="true" class="h-6 w-6" focusable="false" name="mdi:close" />
</span>
<img class="modal-content" id="img01" />
<div id="caption"></div>
</div>
</article>
<style>
.modal {
display: none;
position: fixed;
z-index: 100;
margin: auto;
padding: 50px;
inset: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0, 0, 0, 0.9);
align-items: center;
justify-content: center;
}
.modal-content {
margin: auto;
display: block;
width: 80%;
}
.close {
position: fixed;
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;
}
</style>
<script>
const modal = document.getElementById("myModal");
const modalImg = document.getElementById("img01") as HTMLImageElement;
const imgs = document.querySelectorAll("article img") as NodeListOf<HTMLImageElement>;
imgs.forEach(function (img) {
img.style.cursor = "pointer";
img.addEventListener("click", () => {
if (modal && modalImg) {
modal.style.display = "flex";
document.body.style.overflow = "hidden";
modalImg.src = img.src;
}
});
});
const span = document.getElementsByClassName("close")[0];
span?.addEventListener("click", () => {
if (modal) {
modal.style.display = "none";
document.body.style.overflow = "auto";
}
});
</script>