vue3-自定义指令-图片懒加载
1. 效果展示
效果展示
2. 类型文件 - types/lazyImage.ts
import type {
ObjectDirective } from 'vue'
export type LazyImageDirective = ObjectDirective
export interface MapItem {
intersectionObserver: IntersectionObserver
callback: (...args: any) => void
}
export type IntersectionObserverOptions = {
root: Element | null
rootMargin: string
threshold: number
}
export type ExtraOptions = {
loadingSrc?: string
loadingTime?: number
show: (...args: any) => void
hide: (...args: any) => void
}
export type BindingValue = ExtraOptions & {
src: string
options?: IntersectionObserverOptions
}
export type LazyImageOptions = ExtraOptions & {
options: IntersectionObserverOptions
}
3. 全局配置 config/index.ts
import {
LazyImageOptions} from '@/types/lazyImage'
export interface VDConfig {
lazyImage?: LazyImageOptions
}
const config: VDConfig = {
lazyImage: {
loadingTime: 500,
show: () => {
},
hide: () => {
<