fix: add blur effect when the img is loading

This commit is contained in:
KazooTTT
2025-02-06 00:16:03 +08:00
parent 6d9c280a84
commit 14438ca123
3 changed files with 33 additions and 3 deletions

View File

@ -58,6 +58,24 @@ const { className = "", dataPagefindBody = true } = Astro.props;
text-decoration: none; text-decoration: none;
cursor: pointer; cursor: pointer;
} }
/* 修改图片相关样式 */
:global(article img) {
opacity: 0;
position: relative;
background-color: #f0f0f0;
transition: all 0.5s ease-in-out;
}
:global(article img.loading) {
opacity: 1;
filter: blur(10px);
}
:global(article img.loaded) {
opacity: 1;
filter: blur(0);
}
</style> </style>
<script> <script>
@ -66,12 +84,23 @@ const { className = "", dataPagefindBody = true } = Astro.props;
const imgs = document.querySelectorAll("article img") as NodeListOf<HTMLImageElement>; const imgs = document.querySelectorAll("article img") as NodeListOf<HTMLImageElement>;
imgs.forEach(function (img) { imgs.forEach(function (img) {
// 检查图片是否已经加载完成
if (img.complete) {
img.classList.add("loaded");
} else {
img.classList.add("loading");
img.onload = () => {
img.classList.remove("loading");
img.classList.add("loaded");
};
}
img.style.cursor = "pointer"; img.style.cursor = "pointer";
img.addEventListener("click", () => { img.addEventListener("click", () => {
if (modal && modalImg) { if (modal && modalImg) {
modal.style.display = "flex"; modal.style.display = "flex";
document.body.style.overflow = "hidden"; document.body.style.overflow = "hidden";
modalImg.src = img.src; modalImg.src = img.src;
} }
}); });

View File

@ -3,7 +3,8 @@ import { type CollectionEntry, render } from "astro:content";
import FormattedDate from "@/components/FormattedDate.astro"; import FormattedDate from "@/components/FormattedDate.astro";
import type { HTMLTag, Polymorphic } from "astro/types"; import type { HTMLTag, Polymorphic } from "astro/types";
import GiscusComment from "@/components/componentsBefore/GiscusComment"; import GiscusComment from "@/components/componentsBefore/GiscusComment";
import ArticleContainer from "./ArticleContainer.astro"; import ArticleContainer from "../ArticleContainer.astro";
type Props<Tag extends HTMLTag> = Polymorphic<{ as: Tag }> & { type Props<Tag extends HTMLTag> = Polymorphic<{ as: Tag }> & {
note: CollectionEntry<"note">; note: CollectionEntry<"note">;
isPreview?: boolean | undefined; isPreview?: boolean | undefined;

View File

@ -6,7 +6,7 @@ import TOC from "@/components/blog/TOC.astro";
import WebMentions from "@/components/blog/webmentions/index.astro"; import WebMentions from "@/components/blog/webmentions/index.astro";
import GiscusComment from "@/components/componentsBefore/GiscusComment"; import GiscusComment from "@/components/componentsBefore/GiscusComment";
import BaseLayout from "./Base.astro"; import BaseLayout from "./Base.astro";
import ArticleContainer from "@/components/note/ArticleContainer.astro"; import ArticleContainer from "@/components/ArticleContainer.astro";
interface Props { interface Props {
post: CollectionEntry<"post">; post: CollectionEntry<"post">;