简体   繁体   English

如何使用HTML / javascript以预定义的顺序加载图像?

[英]How to load images in a predefined order with HTML/javascript?

I Have 2 heavy images on my landing page as background. 我的目标网页上有2张沉重的图像作为背景。 They are going to be displayed in a given order so I am trying to load them in that order for a better user experience. 它们将以给定的顺序显示,所以我试图以此顺序加载它们,以获得更好的用户体验。 I tried the code below : 我尝试了下面的代码:

        function loadImages(){
            img1= new Image();
            img2= new Image();

            img1.onLoad=img1Loaded();
            img1.src='img/bg1.jpg';
        }

        function img1Loaded(){
            console.log('image1 loaded');
            window.document.getElementById('page1').style.background= " #009ee3 url(img/bg1.jpg) no-repeat left top"; 
            window.document.getElementById('page1').style.backgroundSize="cover";
            img2.onLoad=img2Loaded();
            img2.src='img/bg2.jpg';
        }
        function img2Loaded(){
            console.log('image2 loaded');
            window.document.getElementById('page2').style.background= ' #009ee3 url(img/bg2.jpg) no-repeat center top';
            window.document.getElementById('page2').style.backgroundSize="cover";

        }

The console messages : 控制台消息:

image1 loaded
image2 loaded

appear in the right order but almost at the same time. 以正确的顺序出现,但几乎同时出现。 After a few seconds, both images are displayed at the same time. 几秒钟后,同时显示两个图像。

What's wrong with my solution and how should I do ? 我的解决方案出了什么问题,怎么办?

Modern web browsers rarely open only a single connection. 现代网络浏览器很少打开单个连接。 After the HTML has downloaded, all resources will be collected and the browser will load many of them in parallel. 下载HTML后,将收集所有资源,浏览器将并行加载其中的许多资源。 This is why your approach probably doesn't help a lot. 这就是为什么您的方法可能无济于事的原因。

The timing you see can have many reasons. 您看到的时间安排可能有很多原因。 The most common of them is that you have opened this page before and you still had the images in the cache. 其中最常见的是您之前已打开此页面,并且图像仍在缓存中。 The browser will then emit a small HEAD request which allows it to determine whether the image in the cache is still "good enough". 然后,浏览器将发出一个小的HEAD请求,该请求使它可以确定缓存中的图像是否仍然“足够好”。

Try again with your caches flushed. 在刷新缓存后重试。

EDIT As devnull69 said, you have two severe bugs in the code: 编辑正如devnull69所说,您在代码中有两个严重的错误:

  1. Setting onLoad has no effect; 设置onLoad无效; the event handler is called onload . 事件处理程序称为onload
  2. Instead of assigning a function reference, you actually call the function. 您实际上没有调用函数引用,而是分配了函数。 Use img1.onload=img1Loaded instead. 请改用img1.onload=img1Loaded

Without these two bug fixes, the code above will: 没有这两个错误修复,上面的代码将:

  1. Call img1Loaded(); 呼叫img1Loaded(); which prints image1 loaded 打印image1 loaded
  2. Call img2Loaded(); 呼叫img2Loaded(); which prints image2 loaded 打印image2 loaded
  3. Assign img2.src (loading the second image). 分配img2.src (加载第二个图像)。 This will actually trigger the loading after the script has returned. 实际上,这将在脚本返回后触发加载。
  4. Assign img1.src (loading the first image, probably in parallel with the other one) 分配img1.src (加载第一个图像,可能与另一个图像并行)

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

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