[英]Load each item after the one before is finished loading in Vue JS
我需要為在線幻燈片演示加載大約 1000 個靜態圖像、.gif 和視頻。 目前所有項目都在一次加載,觀眾需要等待才能看到第一個項目。 如何在前一個項目完成加載后加載每個項目?
Vue.js Vuetify.js 代碼:
<v-row v-for="(objkts, index) in group" :key="index">
<v-col v-for="objkt in objkts" :key="objkt.id">
<v-card :key="index" width="100vh">
<v-img
max-width="100vh"
:src="img(objkt)"
></v-img>
</v-card>
</v-col>
</v-row>
解決方案:
使用圖像列表,只有在加載前一個圖像時,您才需要在每個圖像上動態設置 src 屬性
步驟(5):
<img v-for="(image,index) in group" :key="index" :src="image.src"
<img v-for="(image,index) in group" :key="index" :src="image.src" @load="loadNextimage(index)"/>
loadNextimage(currentIndex){ let nextIndex = currentIndex+1 if( nextIndex < this.group.length){ let img = this.group[nextIndex] img.src = img.asrc delete img.asrc Vue.set(this.group, nextIndex, img) } }
new Vue({ el: '#app', data(){ return { group: [ { id: 1, title: '', asrc: 'https://source.unsplash.com/collection/190727/120x120'}, { id: 2, title: '', asrc: 'https://source.unsplash.com/collection/8961198/120x120'}, { id: 3, title: '', asrc: 'https://source.unsplash.com/collection/190723/120x120'}, { id: 4, title: '', asrc: 'https://source.unsplash.com/collection/KizanWcExgU/120x120'} ] } }, mounted(){ this.loadNextimage(-1) }, methods:{ loadNextimage(currentIndex){ let nextIndex = currentIndex+1 if(nextIndex<this.group.length){ let img = this.group[nextIndex] img.src = img.asrc delete img.asrc Vue.set(this.group,nextIndex,img) } } } });
img { height: 120px; width:120px; } img:not([src]){ background: url(https://www.slntechnologies.com/wp-content/uploads/2017/08/ef3-placeholder-image.jpg); background-size:cover; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script> <div id="app"> <img v-for="(image,index) in group" :key="index" :src="image.src" @load="loadNextimage(index)" > </div>
您可以使用 vuetify 中的lazy
組件: https ://vuetifyjs.com/en/components/lazy/
將您的代碼包裝在此組件中,您的 html 將根據可見性加載。
這類似於 Alphesh 的解決方案,但我認為它更容易閱讀。 這個想法是建立一個具有三個屬性的簡單排隊系統:
您還將有一個名為queueNextImage
的簡單方法,該方法應由mounted
的鈎子調用(以開始第一個圖像加載),然后在圖像完成加載時再次調用,如下所示:
queueNextImage() {
if(this.loadingImage !== null) {
this.loadedImages.push(this.loadingImage)
}
this.loadingImage = this.imagesToLoad.shift()
}
當您啟動imagesToLoad
時,將填充您要加載的所有圖像 URL,並且loadedImages
將是一個空數組。
模板將遍歷loadedImages
的圖像,在常規循環中渲染它們,並將有一個單一的img
元素,其src
屬性綁定到loadingImage
的值,並從圖像的onLoad
事件中觸發queueNextImage
。
完整示例:
<template>
<div>
<p>
Images to load: <span>{{imagesToLoad.length}}</span>
Loading image: <span>{{loadingImage}}</span>
Images Loaded: <span>{{loadedImages.length}}</span>
</p>
<img v-for="item in loadedImages" v-bind:key="item" v-bind:src="item" />
<img v-on:load="queueNextImage" v-bind:src="loadingImage" />
</div>
</template>
<script>
export default {
mounted: function() {
this.queueNextImage();
},
methods: {
queueNextImage() {
if(this.loadingImage !== null) {
this.loadedImages.push(this.loadingImage)
}
this.loadingImage = this.imagesToLoad.shift()
},
},
data: () => ({
loadedImages: [],
loadingImage: null,
imagesToLoad: Array.from({length:200},(v,k)=>`https://via.placeholder.com/${k+850}`),
}),
};
</script>
您需要使用圖像預加載。 如果您使用 Nuxt.js,您可以在構建時加載數據並顯示所有資產。 圖片預加載: https ://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image
您可以在惰性模式下渲染圖像https://css-tricks.com/lazy-loading-images-with-vue-js-directives-and-intersection-observer/
Vuetify 中有一些圖像預加載示例Vuetify carousel image loading
或者你可以使用這個組件來預加載https://github.com/vuetifyjs/vuetify-loader
據我了解,您想要按索引的升序一張一張地加載圖像。
您可以嘗試一種方法,例如:
async
方法並await
循環加載圖像。偽代碼示例:
<v-img
max-width="100vh"
:src="imgCollection[index]"
></v-img>
async function loadImages() {
imagesList.forEach((img, index) => {
var data = await getImageData(img.url);
// update the data into your array
imgCollection[index] = data;
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.