javascript - 我可以检测所有图像何时加载,以便将 isLoaded 变量更改为 true 吗?

标签 javascript vue.js vuejs2

我有以下模板:

<template>
    <div v-if='isLoaded'>
        <div @click='selectSight(index)' v-for='(sight, index) in sights'>
            <img :src="'https://maps.googleapis.com/maps/api/place/photo?maxwidth=300&photoreference=' + sight.photos[0].photo_reference + '&key='">
        </div>
    </div>
</template>

我想知道是否有可能以某种方式检测所有图像何时加载,以便在发生这种情况时将 isLoaded 设置为 true?我想避免在加载所有内容之前显示整个 div,这样我就可以避免加载图像的闪烁(其中一些加载速度更快,一些加载速度更慢)。

<script>
    export default {
            data(){
                return {
                    sights: null,
                    isLoaded: false
                }
            },
            mounted() {
                axios.get('/getSights/' + this.lat + '/' + this.lng + '/' + this.type + '/' + this.range)
                .then(response => {
                    this.sights = response.data.result.results
                    this.nextPageToken = response.data.result.next_page_token
                }).catch((error) => console.log(error));
            }
    }
</script>

我试过:

var images = document.getElementsByClassName('sight-photos');
images.onload = function () {
    console.log('hey')
}

但是我在尝试时没有看到控制台消息:

var images = document.getElementsByClassName('sight-photos')[0];
images.onload = function () {
    console.log('hey')
}

我确实看到了消息,所以我假设您不能对图像集合使用 onload?

最佳答案

如果您使用 v-if 指令,则永远不会创建元素并且不会加载图像。但是,您可以在 div 上使用 v-show 指令来创建 html,但将其隐藏。这里的一种方法是使用数组来跟踪所有加载的图像,然后将用于更新 isLoaded 属性。

<template>
<div v-show='isLoaded'>
    <div @click='selectSight(index)' v-for='(sight, index) in sights'>
        <img 
          :src="'https://maps.googleapis.com/maps/api/place/photo?maxwidth=300&photoreference=' + sight.photos[0].photo_reference + '&key='"  
          v-on:load="setLoaded(index)"
         >
    </div>
</div>

<script>
export default {
        data(){
            return {
                sights: null,
                loadedSights: []
                isLoaded: false
            }
        },
        mounted() {
            axios.get('/getSights/' + this.lat + '/' + this.lng + '/' + this.type + '/' + this.range)
            .then(response => {
                this.sights = response.data.result.results
                this.nextPageToken = response.data.result.next_page_token
                this.loadedSights = this.sights.map(function(){ return false; });
            }).catch((error) => console.log(error));
        },
        methods: {
            setLoaded: function(index){
                this.loadedSights[index] = true;

                for (var i=0; i< this.loadedSights.length; i++){
                    if (!this.loadedSights[i]){ 
                        this.isLoaded = false;
                        return  
                    }
                }
                this.isLoaded = true;
            }
        }
}
</script>

关于javascript - 我可以检测所有图像何时加载,以便将 isLoaded 变量更改为 true 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55757377/

相关文章:

laravel - 在 VueJs 中使用 Laravel 的 Gate/Authorization

javascript - 如何在加载之前获取和删除正文上的脚本

javascript - 如何通过 javascript execCommand 将当前光标中的一行格式化为 <h1> 或 <p> 标签?

vue.js - Vue.js 中的条件 <router-link> 依赖于 prop 值?

javascript - 使用 vue router reactive 使查询参数

javascript - Vue 和带模板标签的条件渲染

javascript - Dojo:TabContainer - 如何在标题中放置菜单按钮?

javascript - .NET MVC Bootstrap : shown. bs.dropdown 不起作用

html - 全局覆盖 Vuetify 分隔线颜色

vue.js - “Cannot read property '在启动vue应用程序时升级' of undefined”