简体   繁体   English

第一次打开页面时图像非常慢-如何加载图像

[英]Images very slow when opening page first time - how to load images

I have a page where the landing page has images that change in time (on by one the images change). 我有一个页面,其中登录页面的图像随时间变化(图像随即变化)。 That works fine, but when uploading the site, I realised that my images at the landing page load very slowly and therefore it does not look good. 效果很好,但是在上载网站时,我意识到登录页面上的图像加载速度非常慢,因此效果不佳。 Once all loaded (all appeared at least once) then page is good again and the transitions smooth and nice. 一旦全部加载(全部至少出现一次),则页面再次良好,过渡平滑而美观。

I was thinkin of a way to preload the images. 我想到了一种预加载图像的方法。 I found many stuff and tried the following: 我发现了很多东西,并尝试了以下方法:

           $(window).load(function() {

               $(".loader").fadeOut("slow");    /*gif that shows when page loads*/

               function backgrounds() {
                    for (i = 0; i < preload.arguments.length; i++) {
                        images[i] = new Image()
                        images[i].src = preload.arguments[i]
                    }
                }

           })

                var backgrounds = new Array(
                    'url(Images/image_1.jpg)'
                  , 'url(Images/image_2.jpg)'
                );

      $(document).ready(function(){
           /*I have here other functions as well as the function that changes the images one by one*/
      });

No success. 没有成功 When loading page first time I have the exact same problem. 第一次加载页面时,我有完全相同的问题。 The photos the first time load extremely slowly and also the change function of the photos starts before they even have time to load (have way of the loading the function that rotates the pics comes in and makes a change). 首次加载照片的速度非常慢,并且甚至在没有时间加载之前就开始更改照片的功能(有一种加载方式,可以旋转照片并进行更改)。

As I am newbie to all this, could anyone please help with what is the best way to tackle the problem, and make sure that the loading is done first. 由于我是这方面的新手,因此任何人都可以帮助解决问题的最佳方法,并确保首先完成加载。

Many thanks 非常感谢

I think you should not use javascript to preload images, since there's a better solution with CSS3 . 我认为您不应该使用javascript来预加载图像,因为CSS3有更好的解决方案。 This is compatible with recent browsers. 这与最近的浏览器兼容。

https://perishablepress.com/preload-images-css3/ https://perishablepress.com/preload-images-css3/

For example: 例如:

.preload-images {
    background: url(image-01.png) no-repeat -9999px -9999px;
    background: url(image-01.png) no-repeat -9999px -9999px,
        url(image-02.png) no-repeat -9999px -9999px,
        url(image-03.png) no-repeat -9999px -9999px,
        url(image-04.png) no-repeat -9999px -9999px,
        url(image-05.png) no-repeat -9999px -9999px;
}

You can create a new image object in the namespace with the necessary file, which will then be found by the browser when it's time to look for the image. 您可以在名称空间中使用必要的文件创建一个新的图像对象,然后当需要查找图像时,浏览器会找到它。 Like this: 像这样:

(new Image()).src = "yourImagePath";

This won't place the image on the page, but it will load it for future use. 这不会将图像放置在页面上,但是会加载该图像以备将来使用。

Hope this helps!! 希望这可以帮助!!

Here is the CSS way to preload the images. 这是预加载图像的CSS方法。

Refer the accepted answer from this link: How to Preload Images without Javascript? 请从以下链接中获得公认的答案: 如何在不使用Javascript的情况下预加载图像?

This will basically load all images on the background, while your showing the loading gif and the actual reference of the image will load it from cache. 基本上,这将在背景上加载所有图像,而显示加载的gif和图像的实际引用将从缓存中加载它。

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

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