简体   繁体   English

下载图像时的加载器

[英]A loader for when images are being downloaded

I am after a pre-loader (a spinning loader, to demonstrate that image is being loaded in the background).我在使用预加载器(旋转加载器,以证明图像正在后台加载)。 This preloader should appear while css backgrounds are being loaded.在加载 css 背景时应该会出现这个预加载器。

Background story: I have a global pre-loader for the SPA itself, so the user sees a loader while the SPA is being downloaded in the background.背景故事:我有一个针对 SPA 本身的全局预加载器,因此用户在后台下载 SPA 时会看到一个加载器。

However, when the user navigates to a different page with images, those new images have to be downloaded by the user's browser and the global pre-loader is not being triggered.但是,当用户导航到带有图像的不同页面时,这些新图像必须由用户的浏览器下载,并且不会触发全局预加载器。

What can I do to show existing loader until all images were downloaded or is there an alternative solution?在下载所有图像之前,我可以做什么来显示现有加载程序,或者是否有替代解决方案?


With jQuery a solution would have been to use $(document).ready , to remove loaded once the document is ready.使用 jQuery 的解决方案是使用$(document).ready ,一旦文档准备好就删除加载。 However, I am struggling to find a solution for it with VueJS.但是,我正在努力使用 VueJS 寻找解决方案。


Currently the solution that I see for is to use something similar to this: https://vuejsexamples.com/a-vue-component-for-showing-loader-during-image-loading/ , but still, this wouldn't handle css background images.目前,我看到的解决方案是使用类似于以下内容的内容: https : //vuejsexamples.com/a-vue-component-for-showing-loader-during-image-loading/ ,但仍然无法处理css 背景图片。


Added code below to the main component.将下面的代码添加到主要组件。 mounted() worked. mounted()工作。 The watcher() display loader but when I added eventListener into the watcher() the listened was not trigger. watcher()显示加载器,但是当我将eventListener添加到watcher() ,监听的不是触发。

export default {
  mounted() {
    window.addEventListener("load", () => {
      let element = document.getElementById("global-loader");
      element.style.display = "none";
    });
  },
  watch: {
    $route(to, from) {
      let element = document.getElementById("global-loader");
      element.style.display = "block";
      // I dont know on how to switch global loader off after page switch took place and images were loaded
    }
  }
};

I have tried using router navigation for this, but addEventListener was not triggered because page is already loaded:我已尝试为此使用路由器导航,但未触发addEventListener ,因为页面已加载:

router.beforeEach((to, from, next) => { 
    let element = document.getElementById("global-loader");
    element.style.display = "block";

});

router.afterEach((to, from, next) => { 
    window.addEventListener("load", () => {
        let element = document.getElementById("global-loader");
        element.style.display = "none";
      });
});

How can I check if a background image is loaded? 如何检查背景图像是否已加载?

Looks like you can't listen to the load event on css background images.看起来您无法在 css 背景图像上收听 load 事件。

What you can do, as described in the linked post, is using a regular <img> tag and listen to its load event.如链接帖子中所述,您可以做的是使用常规<img>标签并侦听其加载事件。 Here's how that could look like in a Vue component: https://codesandbox.io/embed/loading-css-basckground-images-n4nf2这是在 Vue 组件中的样子: https : //codesandbox.io/embed/loading-css-basckground-images-n4nf2

ImageLoadHack.vue is where the magic happens in the above example ImageLoadHack.vue是上面例子中神奇发生的地方

Well, you could always do the same thing inside your Vue component no?好吧,你总是可以在你的 Vue 组件中做同样的事情,不是吗?

I haven't used Vue for a while, but can you do something like:我有一段时间没有使用 Vue,但你能不能做一些类似的事情:

...
mounted() {
    window.addEventListener('load', () => {
         // this is the same thing as "$(document).ready", do whatever you want here. Page is loaded.
    })
},
...

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

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