這篇文章主要介紹“怎么實(shí)現(xiàn)vue-lazyload圖片懶加載”,在日常操作中,相信很多人在怎么實(shí)現(xiàn)vue-lazyload圖片懶加載問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么實(shí)現(xiàn)vue-lazyload圖片懶加載”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
曲周網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,曲周網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為曲周上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的曲周做網(wǎng)站的公司定做!
可以想象一個(gè)網(wǎng)頁打開有成百上千的圖片需要加載,頁面會(huì)變得非常的卡頓,此時(shí)如果只是可視區(qū)域的圖片加載,其他的圖片可以暫時(shí)有一個(gè)占位loading圖,等滾動(dòng)它們到可視區(qū)域時(shí)再去請(qǐng)求真實(shí)圖片并且替換就好了。很好,vue-lazyload插件就是解決此類問題的。
vue-lazyload官網(wǎng)npm配置地址(重要)
https://www.npmjs.com/package/vue-lazyload
npm i vue-lazyload --save
在main.js 文件中 引入 vue-lazyload 的插件。 (全局)
1、最外層static目錄下的圖片引用
import VueLazyLoad from 'vue-lazyload'; // 最外層static目錄下的圖片引用 Vue.use(VueLazyLoad, { error: '/static/images/defaultAvatar.png', // 此處是圖片加載失敗時(shí)候 顯示的圖片 loading: '/static/images/defaultAvatar.png', // 此處是圖片加載中 顯示的圖片 attempt: 1, // 加載一屏圖片 preLoad: 1, // 失敗嘗試次數(shù) });
2、src下的assets目錄下的圖片
import VueLazyLoad from 'vue-lazyload'; // src下的assets目錄下的圖片 Vue.use(VueLazyLoad, { error: require('common/assets/defaultAvatar.png'), // 此處是圖片加載失敗時(shí)候 顯示的圖片 loading: require('common/assets/defaultAvatar.png'), // 此處是圖片加載中 顯示的圖片 attempt: 1, // 加載一屏圖片 preLoad: 1, // 失敗嘗試次數(shù) });
*項(xiàng)目使用遇到問題,說明:
import VueLazyLoad from 'vue-lazyload';
報(bào)錯(cuò):Absolute imports should come before relative imports.
原因:主要是引入文件的位置問題。
只需要在動(dòng)態(tài)請(qǐng)求img 路徑 把原本的 :scr="url", 替換為 v-lazy="url" 接下來,再去看看效果實(shí)例。
lazyload的主要流程的流程圖
1)vue-lazyload是通過指令的方式實(shí)現(xiàn)的,定義的指令是v-lazy指令;
2)指令被bind時(shí)會(huì)創(chuàng)建一個(gè)listener,并將其添加到listener queue里面, 并且搜索target dom節(jié)點(diǎn),為其注冊(cè)dom事件(如scroll事件);
3)上面的dom事件回調(diào)中,會(huì)遍歷 listener queue里的listener,判斷此listener綁定的dom是否處于頁面中perload的位置,如果處于則加載異步加載當(dāng)前圖片的資源;
4)同時(shí)listener會(huì)在當(dāng)前圖片加載的過程的loading,loaded,error三種狀態(tài)觸發(fā)當(dāng)前dom渲染的函數(shù),分別渲染三種狀態(tài)下dom的內(nèi)容;
1、在組件install安裝時(shí),調(diào)用LazyClass返回了一個(gè)class對(duì)象,然后創(chuàng)建了一個(gè)class實(shí)例。
2、其核心是lazyLoadHandler()函數(shù),是經(jīng)過節(jié)流函數(shù)處理的圖片加載的入口函數(shù)。
this.lazyLoadHandler = throttle(() => { let catIn = false this.ListenerQueue.forEach(listener => { if (listener.state.loaded) return catIn = listener.checkInView() catIn && listener.load() }) }, 200) checkInView () { this.getRect() // 調(diào)用dom的getBoundingClientRect() return (this.rect.top < window.innerHeight * this.options.preLoad && this.rect.bottom > this.options.preLoadTop) && (this.rect.left < window.innerWidth * this.options.preLoad && this.rect.right > 0) // 判斷dom的頂部是否到了preload的位置,判斷dom的底部是否到達(dá)了preload的位置,X軸同理。 }
3、主要操作:找到對(duì)應(yīng)的target(用于注冊(cè)dom事件的dom節(jié)點(diǎn);比如:頁面滾動(dòng)的dom節(jié)點(diǎn)),為其注冊(cè)dom事件;為當(dāng)前dom創(chuàng)建Listenr并添加到listener queue中。最后用lazyLoadHandler()函數(shù),加載圖片。
4、當(dāng)滿足條件,調(diào)用load()函數(shù)異步加載圖片。
load () { // 如果當(dāng)前嘗試加載圖片的次數(shù)大于指定的次數(shù), 并且當(dāng)前狀態(tài)還是錯(cuò)誤的, 停止加載動(dòng)作 if ((this.attempt > this.options.attempt - 1) && this.state.error) { if (!this.options.silent) console.log('error end') return } if (this.state.loaded || imageCache[this.src]) { //如果已緩存 return this.render('loaded', true) // 使用緩存渲染圖片 } this.render('loading', false) // 調(diào)用lazy中的 elRender()函數(shù), 用戶切換img的src顯示數(shù)據(jù),并觸發(fā)相應(yīng)的狀態(tài)的回調(diào)函數(shù) this.attempt++ // 嘗試次數(shù)累加 this.record('loadStart') // 記錄當(dāng)前狀態(tài)的時(shí)間 // 異步加載圖片, 使用Image對(duì)象實(shí)現(xiàn) loadImageAsync({ src: this.src }, data => { this.naturalHeight = data.naturalHeight this.naturalWidth = data.naturalWidth this.state.loaded = true this.state.error = false this.record('loadEnd') this.render('loaded', false) // 渲染 loaded狀態(tài)的 dom的內(nèi)容 imageCache[this.src] = 1 // 當(dāng)前圖片緩存在瀏覽器里面了 }, err => { this.state.error = true this.state.loaded = false this.render('error', false) }) }
5、loadImageAsync異步加載圖片方法,通過image對(duì)象實(shí)現(xiàn)的網(wǎng)絡(luò)請(qǐng)求。
const loadImageAsync = (item, resolve, reject) => { let image = new Image() image.src = item.src image.onload = function () { resolve({ naturalHeight: image.naturalHeight, // 圖片的 實(shí)際高度 naturalWidth: image.naturalWidth, src: image.src }) } image.onerror = function (e) { reject(e) } }
6、lazy class的update()函數(shù),也就是v-lazy指令綁定的數(shù)據(jù)發(fā)生改變的時(shí)候出發(fā)的回調(diào)函數(shù)。
update (el, binding) { // 獲取當(dāng)前dom綁定的 圖片src的數(shù)據(jù), 如果當(dāng)前dom執(zhí)行過load過程, 重置當(dāng)前dom的圖片數(shù)據(jù)和狀態(tài) let { src, loading, error } = this.valueFormatter(binding.value) // 當(dāng)前綁定的value是 obj, 從中選取{src, loading, error}; 是string, 則用作src // 找到當(dāng)前dom綁定的listener const exist = find(this.ListenerQueue, item => item.el === el) // 更新listener的狀態(tài)和狀態(tài)對(duì)應(yīng)的圖片資源 exist && exist.update({ src, loading, error }) this.lazyLoadHandler() Vue.nextTick(() => this.lazyLoadHandler()) }
到此,關(guān)于“怎么實(shí)現(xiàn)vue-lazyload圖片懶加載”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
文章標(biāo)題:怎么實(shí)現(xiàn)vue-lazyload圖片懶加載
URL分享:http://chinadenli.net/article48/gigchp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、云服務(wù)器、、軟件開發(fā)、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)