@@ -3,12 +3,6 @@ import cn from "classnames";
33import { Component , ComponentProps , JSX } from "solid-js" ;
44import { breakpoint , withStyle } from "~/style/commonStyle" ;
55
6- const isImageCached = ( url : string ) => {
7- const img = new Image ( ) ;
8- img . src = url ;
9- return img . complete ;
10- } ;
11-
126interface Props extends ComponentProps < "img" > {
137 style ?: JSX . CSSProperties ;
148 class ?: string ;
@@ -24,12 +18,13 @@ export interface LazyImageMeta {
2418const LazyImage : ( meta : LazyImageMeta ) => Component < Props > =
2519 ( meta ) => ( props ) => {
2620 const { class : $class , style, ...rest } = $destructure ( props ) ;
21+ let imageRef ! : HTMLImageElement ;
2722 let hasJS = $signal ( false ) ;
2823 let loaded = $signal ( false ) ;
2924
3025 $effect ( ( ) => {
3126 hasJS = true ;
32- loaded = isImageCached ( meta . url ) ;
27+ loaded ||= imageRef . complete ;
3328 } ) ;
3429
3530 return (
@@ -43,11 +38,10 @@ const LazyImage: (meta: LazyImageMeta) => Component<Props> =
4338 "--color2" : meta . gradient [ 0 ] ,
4439 } } >
4540 < img
41+ ref = { imageRef }
4642 class = { cn ( { hasJS, loaded } ) }
43+ onLoad = { ( ) => { loaded = true ; } }
4744 src = { meta . url }
48- onload = { ( ) => {
49- loaded = true ;
50- } }
5145 { ...rest }
5246 />
5347 </ div >
@@ -76,11 +70,11 @@ const _LazyImage = css`
7670 height : 100% ;
7771 object-fit : inherit;
7872 // hide text when loading image
79- text-indent : 100 % ;
73+ font-size : 0 ;
8074
8175 @supports (animation-name : fadeIn) {
8276 opacity : 0 ;
83- animation : fadeIn 200ms 4 s forwards;
77+ animation : fadeIn 200ms 1 s forwards;
8478 }
8579 @keyframes fadeIn {
8680 0% {
0 commit comments