日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

vue3+typescript實現圖片懶加載插件

瀏覽:46日期:2022-11-12 08:42:42

github項目地址: github.com/murongg/vue…

求star 與 issues

我文采不好,可能寫的文章不咋樣,有什么問題可以在留言區評論,我會盡力解答

本項目已經發布到npm

安裝:

$ npm i vue3-lazyload# or$ yarn add vue3-lazyload

需求分析

支持自定義 loading 圖片,圖片加載狀態時使用此圖片 支持自定義 error 圖片,圖片加載失敗后使用此圖片 支持 lifecycle hooks,類似于 vue 的生命周期,并同時在 img 標簽綁定 lazy 屬性,類似于

<img src='http://www.b3g6.com/bcjs/...' lazy='loading'><img src='http://www.b3g6.com/bcjs/...' lazy='loaded'><img src='http://www.b3g6.com/bcjs/...' lazy='error'>

并支持:

img[lazy=loading] { /*your style here*/ } img[lazy=error] { /*your style here*/ } img[lazy=loaded] { /*your style here*/ }

支持使用 v-lazy 自定義指令,指定可傳入 string/object ,當為 string 時,默認為需要加載的 url,當為 object 時,可傳入

src: 當前需要加載的圖片 url loading: 加載狀態時所用到的圖片 error: 加載失敗時所用到的圖片 lifecycle: 本次 lazy 的生命周期,替換掉全局生命周期

目錄結構

- src---- index.ts 入口文件,主要用來注冊插件---- lazy.ts 懶加載主要功能---- types.ts 類型文件,包括 interface/type/enum 等等---- util.ts 共享工具文件

編寫懶加載類

懶加載主要通過 IntersectionObserver對象實現,可能有些瀏覽器不支持,暫未做兼容。

確定注冊插件時傳入的參數

export interface LazyOptions { error?: string; // 加載失敗時的圖片 loading?: string; // 加載中的圖片 observerOptions?: IntersectionObserverInit; // IntersectionObserver 對象傳入的第二個參數 log?: boolean; // 是否需要打印日志 lifecycle?: Lifecycle; // 生命周期 hooks}export interface ValueFormatterObject { src: string, error?: string, loading?: string, lifecycle?: Lifecycle;}export enum LifecycleEnum { LOADING = ’loading’, LOADED = ’loaded’, ERROR = ’error’}export type Lifecycle = { [x in LifecycleEnum]?: () => void;};

確定類的框架

vue3 的 Custom Directives,支持以下 Hook Functions:beforeMount 、mounted、beforeUpdate、updated、beforeUnmount、unmounted,具體釋義可以去 vue3 文檔查看,目前僅需要用到mounted、updated、unmounted,這三個 Hook。

Lazy 類基礎框架代碼,lazy.ts:

export default class Lazy { public options: LazyOptions = { loading: DEFAULT_LOADING, error: DEFAULT_ERROR, observerOptions: DEFAULT_OBSERVER_OPTIONS, log: true, lifecycle: {} }; constructor(options?: LazyOptions) { this.config(options) } /** * merge config * assgin 方法在 util.ts 文件內,此文章不在贅述此方法代碼,可在后文 github 倉庫內查看此代碼 * 此方法主要功能是合并兩個對象 * * @param {*} [options={}] * @memberof Lazy */ public config(options = {}): void { assign(this.options, options) } public mount(el: HTMLElement, binding: DirectiveBinding<string | ValueFormatterObject>): void {} // 對應 directive mount hook public update() {} // 對應 directive update hook public unmount() {} // 對應 directive unmount hook}

編寫懶加載功能

/** * mount * * @param {HTMLElement} el * @param {DirectiveBinding<string>} binding * @memberof Lazy */ public mount(el: HTMLElement, binding: DirectiveBinding<string | ValueFormatterObject>): void { this._image = el const { src, loading, error, lifecycle } = this._valueFormatter(binding.value) this._lifecycle(LifecycleEnum.LOADING, lifecycle) this._image.setAttribute(’src’, loading || DEFAULT_LOADING) if (!hasIntersectionObserver) { this.loadImages(el, src, error, lifecycle) this._log(() => { throw new Error(’Not support IntersectionObserver!’) }) } this._initIntersectionObserver(el, src, error, lifecycle) } /** * force loading * * @param {HTMLElement} el * @param {string} src * @memberof Lazy */ public loadImages(el: HTMLElement, src: string, error?: string, lifecycle?: Lifecycle): void { this._setImageSrc(el, src, error, lifecycle) } /** * set img tag src * * @private * @param {HTMLElement} el * @param {string} src * @memberof Lazy */ private _setImageSrc(el: HTMLElement, src: string, error?: string, lifecycle?: Lifecycle): void { const srcset = el.getAttribute(’srcset’) if (’img’ === el.tagName.toLowerCase()) { if (src) el.setAttribute(’src’, src) if (srcset) el.setAttribute(’srcset’, srcset) this._listenImageStatus(el as HTMLImageElement, () => { this._log(() => { console.log(’Image loaded successfully!’) }) this._lifecycle(LifecycleEnum.LOADED, lifecycle) }, () => { // Fix onload trigger twice, clear onload event // Reload on update el.onload = null this._lifecycle(LifecycleEnum.ERROR, lifecycle) this._observer.disconnect() if (error) el.setAttribute(’src’, error) this._log(() => { throw new Error(’Image failed to load!’) }) }) } else { el.style.backgroundImage = ’url(’’ + src + ’’)’ } } /** * init IntersectionObserver * * @private * @param {HTMLElement} el * @param {string} src * @memberof Lazy */ private _initIntersectionObserver(el: HTMLElement, src: string, error?: string, lifecycle?: Lifecycle): void { const observerOptions = this.options.observerOptions this._observer = new IntersectionObserver((entries) => { Array.prototype.forEach.call(entries, (entry) => { if (entry.isIntersecting) { this._observer.unobserve(entry.target) this._setImageSrc(el, src, error, lifecycle) } }) }, observerOptions) this._observer.observe(this._image) } /** * only listen to image status * * @private * @param {string} src * @param {(string | null)} cors * @param {() => void} success * @param {() => void} error * @memberof Lazy */ private _listenImageStatus(image: HTMLImageElement, success: ((this: GlobalEventHandlers, ev: Event) => any) | null, error: OnErrorEventHandler) { image.onload = success image.onerror = error } /** * to do it differently for object and string * * @public * @param {(ValueFormatterObject | string)} value * @returns {*} * @memberof Lazy */ public _valueFormatter(value: ValueFormatterObject | string): ValueFormatterObject { let src = value as string let loading = this.options.loading let error = this.options.error let lifecycle = this.options.lifecycle if (isObject(value)) { src = (value as ValueFormatterObject).src loading = (value as ValueFormatterObject).loading || this.options.loading error = (value as ValueFormatterObject).error || this.options.error lifecycle = ((value as ValueFormatterObject).lifecycle || this.options.lifecycle) } return { src, loading, error, lifecycle } } /** * log * * @param {() => void} callback * @memberof Lazy */ public _log(callback: () => void): void { if (!this.options.log) { callback() } } /** * lifecycle easy * * @private * @param {LifecycleEnum} life * @param {Lifecycle} [lifecycle] * @memberof Lazy */ private _lifecycle(life: LifecycleEnum, lifecycle?: Lifecycle): void {switch (life) { case LifecycleEnum.LOADING: this._image.setAttribute(’lazy’, LifecycleEnum.LOADING) if (lifecycle?.loading) { lifecycle.loading() } break case LifecycleEnum.LOADED: this._image.setAttribute(’lazy’, LifecycleEnum.LOADED) if (lifecycle?.loaded) { lifecycle.loaded() } break case LifecycleEnum.ERROR: this._image.setAttribute(’lazy’, LifecycleEnum.ERROR) if (lifecycle?.error) { lifecycle.error() } break default: break } }

編寫 update hook

/** * update * * @param {HTMLElement} el * @memberof Lazy */ public update(el: HTMLElement, binding: DirectiveBinding<string | ValueFormatterObject>): void { this._observer.unobserve(el) const { src, error, lifecycle } = this._valueFormatter(binding.value) this._initIntersectionObserver(el, src, error, lifecycle) }

編寫 unmount hook

/** * unmount * * @param {HTMLElement} el * @memberof Lazy */ public unmount(el: HTMLElement): void { this._observer.unobserve(el) }

在 index.ts 編寫注冊插件需要用到的 install 方法

import Lazy from ’./lazy’import { App } from ’vue’import { LazyOptions } from ’./types’export default { /** * install plugin * * @param {App} Vue * @param {LazyOptions} options */ install (Vue: App, options: LazyOptions): void { const lazy = new Lazy(options) Vue.config.globalProperties.$Lazyload = lazy // 留著備用,為了兼容$Lazyload // 選項api,可以通過this.$Lazyload獲取到Lazy類的實例,組合api我還不知道怎么獲取 // 所以通過 provide 來實現此需求 // 使用方式 const useLazylaod = inject(’Lazyload’) Vue.provide(’Lazyload’, lazy) Vue.directive(’lazy’, { mounted: lazy.mount.bind(lazy), updated: lazy.update.bind(lazy), unmounted: lazy.unmount.bind(lazy) }) }}

使用插件

import { createApp } from ’vue’import App from ’./App.vue’import VueLazyLoad from ’../src/index’const app = createApp(App)app.use(VueLazyLoad, { log: true, lifecycle: { loading: () => { console.log(’loading’) }, error: () => { console.log(’error’) }, loaded: () => { console.log(’loaded’) } }})app.mount(’#app’)

App.vue:

<template> <div /> <img v-lazy='’/example/assets/logo.png’' alt='Vue logo' width='100'> <img v-lazy='{src: errorlazy.src, lifecycle: errorlazy.lifecycle}' alt='Vue logo' width='100'> <button @click='change'> change </button></template><script>import { reactive } from ’vue’export default { name: ’App’, setup() { const errorlazy = reactive({ src: ’/example/assets/log1o.png’, lifecycle: { loading: () => { console.log(’image loading’) }, error: () => { console.log(’image error’) }, loaded: () => { console.log(’image loaded’) } } }) const change = () => { errorlazy.src = ’http://t8.baidu.com/it/u=3571592872,3353494284&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1603764281&t=bedd2d52d62e141cbb08c462183601c7’ } return { errorlazy, change } }}</script><style>.margin { margin-top: 1000px;}.image[lazy=loading] { background: goldenrod;}.image[lazy=error] { background: red;}.image[lazy=loaded] { background: green;}</style>

以上就是vue3+typescript實現圖片懶加載插件的詳細內容,更多關于vue3 圖片懶加載的資料請關注好吧啦網其它相關文章!

標簽: Vue
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
日韩欧美自拍| 欧美一区自拍| 免费看一区二区三区| 亚洲另类视频| 男人的天堂久久精品| 中文字幕在线看片| 精品国产精品国产偷麻豆| 久久精品欧洲| 人在线成免费视频| av在线日韩| 免费久久久久久久久| 亚洲激情黄色| 蜜桃av一区| 久久亚洲风情| 日韩欧美中文字幕电影| 日本高清久久| 国产亚洲精品美女久久久久久久久久| 婷婷综合成人| 国产欧美日韩精品一区二区三区| 国产成人精品免费视| 成人午夜网址| 国产伦久视频在线观看| 欧洲一区二区三区精品| 五月婷婷六月综合| 91久久视频| 日韩精选在线| 乱一区二区av| 狠狠躁少妇一区二区三区| 日韩欧美另类一区二区| 女人天堂亚洲aⅴ在线观看| 欧美专区18| 91嫩草精品| 国产不卡精品在线| 婷婷亚洲五月色综合| 日韩精品一二三区| 亚洲欧洲美洲国产香蕉| 国产美女视频一区二区| 午夜影院一区| 亚洲尤物在线| 欧美交a欧美精品喷水| 蜜臀国产一区| 久久av一区二区三区| 国产精品中文| re久久精品视频| 青草综合视频| 日韩久久精品| 亚洲深深色噜噜狠狠爱网站 | 精品在线播放| 亚洲综合福利| 精品中文在线| 黄色国产精品| 国产精品视频一区二区三区综合| 日韩一区二区中文| 亚洲精品一区二区在线看| 日韩av资源网| 日韩中文在线播放| 在线免费观看亚洲| 老司机精品视频网| 亚洲二区三区不卡| 国产精品亚洲四区在线观看 | 国产日韩在线观看视频| 韩国三级一区| 日本a级不卡| 日本韩国欧美超级黄在线观看| 日韩精品一二三区| 一区二区精品伦理...| 欧美一区三区| 国产精品亚洲综合久久| 在线综合视频| 国产一区二区三区四区五区传媒| 日韩在线一二三区| 高清不卡亚洲| 91伊人久久| 日韩三区免费| 国产精品尤物| 午夜在线精品| 亚洲精品在线影院| 一级欧洲+日本+国产| 国内自拍视频一区二区三区| 在线精品观看| 久久久久网站| 久久精品国产精品亚洲毛片| 欧美中文日韩| 尤物tv在线精品| 精品视频高潮| 奇米777国产一区国产二区| 最新国产拍偷乱拍精品| 欧美日韩视频网站| 国产九九精品| 国产精品腿扒开做爽爽爽挤奶网站| 日本久久综合| 国产精品3区| 香蕉久久久久久| 激情婷婷综合| 日韩一区二区三区免费| 久久亚洲精品中文字幕| 日韩中文字幕亚洲一区二区va在线| 久久婷婷亚洲| 国产一区二区三区四区| 国产亚洲精品精品国产亚洲综合| 久久午夜精品| 亚洲在线电影| 欧美网站在线| 亚洲特级毛片| 国产专区精品| 国产精品夜夜夜| 青青草精品视频| 午夜性色一区二区三区免费视频| 午夜久久tv| 色婷婷精品视频| 国产一区二区久久久久| 麻豆精品在线观看| 久久国产欧美日韩精品| 日韩1区2区3区| 91成人在线网站| 四虎在线精品| 视频精品一区| 日韩欧美激情| 日韩中文av| 鲁大师成人一区二区三区| 日韩亚洲国产欧美| aⅴ色国产欧美| 免费毛片在线不卡| 五月天久久久| 免费在线看一区| 中文精品电影| 美女精品网站| 亚洲97av| 日韩精品国产欧美| 日韩国产一二三区| 午夜精品影视国产一区在线麻豆| 亚洲精一区二区三区| 亚洲在线久久| 日韩精品中文字幕吗一区二区| 一级欧美视频| 日本a级不卡| 国产日韩欧美一区二区三区| 国产精品密蕾丝视频下载| 欧美交a欧美精品喷水| 精品美女视频| 日本久久黄色| 亚洲午夜91| 久久亚洲国产精品一区二区| 视频在线观看91| 日本不卡一区二区三区| 欧美亚洲一区二区三区| 国产精品videossex| 91综合视频| 99视频精品全国免费| 免费在线小视频| 国产视频亚洲| 四虎精品永久免费| 国产精品成人自拍| 久久男人天堂| 1024精品久久久久久久久| 爽好多水快深点欧美视频| 日韩国产在线观看一区| 成人在线免费观看91| 欧美日韩国产高清电影| 亚洲婷婷丁香| 久久伊人国产| 欧美+亚洲+精品+三区| 亚洲精品国模| 精品视频在线你懂得| 韩国精品主播一区二区在线观看| 尹人成人综合网| 国产区精品区| 亚洲a一区二区三区| 蜜桃久久精品一区二区| 欧美精品1区| 午夜精品亚洲| 国产精品白丝一区二区三区| 亚洲福利一区| 欧美三区不卡| 国产99久久久国产精品成人免费| 日韩一区二区三区免费视频| 精品国产美女a久久9999| 亚洲成人三区| 国产精品久av福利在线观看| 综合日韩av| 综合激情五月婷婷| 日韩av在线中文字幕| 麻豆久久精品| 欧美xxxx中国| 综合一区在线| 久久男人av资源站| 免费看黄色91| 精品国产亚洲一区二区三区在线 | 国产在视频一区二区三区吞精| 欧美搞黄网站| av高清一区| 日韩高清不卡在线| 久久精品亚洲欧美日韩精品中文字幕| 欧美日韩一二三四| 国产精品大片| 日韩中文字幕麻豆| 精品美女视频| 日韩中文av| 国产suv精品一区| 天海翼亚洲一区二区三区|