Skip to content

Commit

Permalink
懒加载图片组件
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Nov 21, 2024
1 parent fb96bc7 commit a07b128
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 13 additions & 9 deletions components/LazyImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export default function LazyImage({
if (typeof onLoad === 'function') {
onLoad() // 触发传递的onLoad回调函数
}
// 移除占位符类名
if (imageRef.current) {
imageRef.current.classList.remove('lazy-image-placeholder')
}
}
/**
* 图片加载失败回调
Expand All @@ -53,6 +57,7 @@ export default function LazyImage({
} else {
imageRef.current.src = defaultPlaceholderSrc
}
imageRef.current.classList.remove('lazy-image-placeholder')
}
}

Expand Down Expand Up @@ -141,15 +146,14 @@ export default function LazyImage({
<style>
{`
.lazy-image-placeholder{
background:
linear-gradient(90deg,#0001 33%,#0005 50%,#0001 66%)
#f2f2f2;
background-size:300% 100%;
animation: l1 1s infinite linear;
}
@keyframes l1 {
0% {background-position: right}
background:
linear-gradient(90deg,#0001 33%,#0005 50%,#0001 66%)
#f2f2f2;
background-size:300% 100%;
animation: l1 1s infinite linear;
}
@keyframes l1 {
0% {background-position: right}
}
`}
</style>
Expand Down
2 changes: 1 addition & 1 deletion themes/magzine/components/PostItemCardSimple.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import CategoryItem from './CategoryItem'
* @param {*} param0
* @returns
*/
const PostItemCardSimple = ({ post, showSummary }) => {
const PostItemCardSimple = ({ post }) => {
return (
<div
key={post.id}
Expand Down

0 comments on commit a07b128

Please sign in to comment.