简体   繁体   English

vuejs渲染完所有图片后执行代码

[英]Execute code after rendering all images in vuejs

I want to do some code execution after all images are loaded (need the set scroll in a specific position).我想在加载所有图像后执行一些代码(需要在特定位置设置滚动)。 I use nextTik() but Codes are processed before loading images .我使用 nextTik() 但在加载图像之前处理代码。 We also can't use mounted and created and methods like this because codes should be executed after clicking on a button.我们也不能使用 mount 和 created 以及这样的方法,因为代码应该在点击按钮后执行。 is there any method or trick?有什么方法或技巧吗? thanks谢谢

You can use the load event on each image to check if all images have been loaded.您可以在每个图像上使用load事件来检查是否所有图像都已加载。

example: https://jsfiddle.net/jacobgoh101/e5bd86k6/1/示例: https : //jsfiddle.net/jacobgoh101/e5bd86k6/1/

<div id="app">
  <img @load="handleLoad"  @error="handleLoad" v-for="i in 10" :src="`https://picsum.photos/${Math.random()*300}/700/?random`"/>
</div>

Javascript Javascript

new Vue({
  el: "#app",
  data: {
    imgLoaded: 0
  },
  methods: {
    handleLoad: function(todo){
        this.imgLoaded++;
      if(this.imgLoaded === 10) {
        alert('all image loaded')   
      }
    }
  }
})

Here is my approach for a component having both "static" images and an an arbitray number of images fetched from database :这是我对具有“静态”图像和从数据库中获取的任意数量的图像的组件的方法:

  • Append two items to the component data: 'imgsLength' and 'loadedImgsLength'将两项附加到组件数据:'imgsLength' 和 'loadedImgsLength'
  • Append a 'load' event handler to every img element, that increments 'loadedImgsLength'将“load”事件处理程序附加到每个 img 元素,增加“loadedImgsLength”
  • After all fetched data are loaded, set 'imgsLength' to the number of all img elements加载所有获取的数据后,将 'imgsLength' 设置为所有 img 元素的数量

So, you know that all images are loaded when 'loadedImgsLength' > 0 and 'loadedImgsLength' == 'imgsLength'因此,您知道所有图像在 'loadedImgsLength' > 0 和 'loadedImgsLength' == 'imgsLength' 时加载

In this exemple, my component won't be visible until all images are loaded:在此示例中,在加载所有图像之前,我的组件将不可见:

<template>
   <div v-show="ready" ref="myComponent">
      ...
      <img src="..." @load="loadedImgsLength++" />  (static image)
      ...
      <img v-for="img in fetchedImg" @load="loadedImgsLength++" />  (dynamic images)
   </div>
</template>

<script>
...
   data(){
      return {
         ...,
         imgsLength : 0,
         loadedImgsLength: 0,
         ...,
      }
   },

   computed: {
     ready(){
        return (this.loadedImgsLength > 0 && this.loadedImgsLength == this.imgsLength)
      }
   },

   async created(){
      ... fetch all data
      this.$nextTick(() => this.$nextTick(this.imgsLength = this.$refs.myComponent.getElementsByTagName("img").length))
   }

         

Based on ( https://stackoverflow.com/a/50481269 enter link description here ) answer I did something like this:基于( https://stackoverflow.com/a/50481269在此处输入链接描述)回答我做了这样的事情:

    <img class="card-img-top" :src=" 'images/' + (data.src || 'image-default.png') " 
    :alt="data.alt" @load="handleImagesLoad" @error="handleImagesLoad">

(@load and @error on each image on my page) and (@load 和 @error 在我页面上的每个图像上)和

   handleImagesLoad: function($event){
       var images = $('img:not([loaded])');
       var target = $($event.target);
       $(target).attr('loaded', true);

       if(images.length <= 1){
           $('#preloader').fadeOut();
       }
   },  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM