| Name | Type | Optional | Description |
|---|---|---|---|
| class | string | true | 类名 |
| style | CSSProperties | true | 样式 |
| src | string | true | 图片地址 |
| defaultSrc | string | true | 图片默认地址 |
| errorSrc | string | true | 出错的时候的图片地址 |
| srcTpl | ImgSrcTplPropFn | true | 可以单个组件自定义,比如
|
| statusClassNamePrefix | string | true | 图片不同状态下的样式名前缀 |
| lazy | "scroll" | "resize" | "off" | true | 懒加载模式
|
| shouldUpdate | (newRect: DOMRect, oldRect: DOMRect) => boolean | true | 当 lazy === 'resize' 时,判断是否要更新图片 默认当面积变大至少20%,才更新图片 |
| loadingType | "src" | "css" | "none" | true | 设置图片为异步加载 这个时候,useImg 返回的 state.status 可能会 blank -> loading -> loaded 或者 blank -> loading -> error 当 loadingType === 'src' 时,状态会体现在 state.src 上,
|
| prepareImg | ( imgSrc: string, crossOrigin?: 'anonymous' | 'use-credentials' | '' ) => Promise<HTMLImageElement> | true | 预处理图片
注意当
|
| crossOrigin | "" | "anonymous" | "use-credentials" | true | 注意当 |
| onError | (e: string | Event) => void | true | 加载失败时调用
注意当 |
| onLoaded | (img: HTMLImageElement) => void | true | 加载成功时调用
注意当 |
| Name | Type | Optional | Description |
|---|---|---|---|
| src | string | false | 真实使用的 src |
| originSrc | string | false | 未处理前的 src |
| status | "blank" | "loading" | "error" | "loaded" | false | 当前状态 |
| rect | DOMRect | true | 图片节点的 DOMRect |
useImg 返回值
| Name | Type | Optional | Description |
|---|---|---|---|
| domRef | Ref<T | undefined> | false | 获取节点 DOM ref |
| state | ImgState | false | 组件状态 |
| imgPool | Ref<ImgPool> | false | 当前使用的 imgPool |
| domProps | Pick<ImgProps, "class" | "crossOrigin"> | false | 处理后的 DOM 节点属性 |
| getDefaultSrc | () => string | false | 获取默认图片 |
src tpl 全局设置
| Name | Type | Optional | Description |
|---|---|---|---|
| webp | false | true | false | 是否使用 webp 格式的图片 |
| ratio | number | false | 设备像素比 |
| Name | Type | Optional | Description |
|---|---|---|---|
| rect | DOMRect | false | 对应 dom 节点的 DOMRect |
| src | string | false | img.src |
| Name | Type | Optional | Description |
|---|---|---|---|
| ratioWidth | number | false | 返回按比例放大图片的 width |
| ratioHeight | number | false | 返回按比例放大图片的 height |
| webp | false | true | false | 是否使用 webp 格式的图片 |
| ratio | number | false | 设备像素比 |
| rect | DOMRect | false | 对应 dom 节点的 DOMRect |
| src | string | false | img.src |
| Name | Type | Optional | Description |
|---|---|---|---|
| defaultSrc | string | true | 全局默认图片 |
| errorSrc | string | true | 图片加载出错时的全局默认设置图片 |
| loadingType | "src" | "css" | true | 默认加载设置 |
| className | string | true | 全局样式名 |
| statusClassNamePrefix | string | true | 图片不同状态下的样式名前缀 |
| shouldUpdate | (newRect: DOMRect, oldRect: DOMRect) => boolean | true | 当 lazy === 'resize' 时,判断是否要更新图片 默认当 width 或者 height 变大至少20%,才更新图片 |
| Name | Type | Optional | Description |
|---|---|---|---|
| container | HTMLElement | Window | true | 容器节点,也可以是 window |
| getContainerRect | (rect: DOMRect) => ImgRect | true | 设置需要检测的容器区域 |
| tickTime | number | true | 心跳间隔时间,单位 ms |
| createSrcTpl | ImgSrcTplFactoryResult | true | 全局的 srcTpl 设置 |
| globalVars | ImgPoolGlobals | true | 一些全局变量 |
| name | string | true | 名字,用于一些问题的排查 |
| Name | Type | Optional | Description |
|---|---|---|---|
| imgs | Set<ImgItem> | false | 所有图片实例 |
| currentEventType | "scroll" | "resize" | "none" | "scroll+resize" | false | 当前发生事件类型 |
| containerRect | ImgRect | false | 容器检测区域 |
| tickTime | number | false | 心跳间隔时间 |
| container | HTMLElement | Window | false | 容器节点 |
| globalVars | ImgPoolGlobals | false | 全局设置 |
| name | string | false | 名称 |
| isOverlapWindow | false | true | false | 容器如果是 DOM 节点,判断容器是否和 window 有重叠 |
| srcTpl | ImgSrcTpl | false | src 模板 |
| overlap | (rect: ImgRect) => boolean | false | 判断图片所在矩形区域与容器区域是否有重叠 |
| reset | (opts: ImgPoolOptions) => void | false | 重置 |
| init | () => void | false | 初始化 |
| destroy | () => void | false | 销毁 |
| occur | (type: ImgEventType) => void | false | 发生了某个事件 |
| update | () => void | false | 心跳更新函数 |
| tick | (isStart?: boolean) => void | false | 执行心跳 |
| stopTick | () => void | false | 停止心跳 |
| appendDefaultStyle | () => void | false | 根据 globalVars.className 设置一个全局默认样式 默认样式为:
|
| Name | Type | Optional | Description |
|---|---|---|---|
| shouldCheck | false | true | false | 当前实例是否需要检测 |
| onChecked | (event: ImgEventType) => void | false | 触发检测时调用 |
| onUpdate | () => void | true | 每次心跳时执行 |