有没有办法“控制”预加载图像(也许在 jQuery 的帮助下)?

发布于 2024-11-28 19:22:56 字数 843 浏览 2 评论 0原文

对于某种幻灯片,我正在尝试实现一个巧妙的图像预加载器,其工作原理如下:

  1. 加载幻灯片时显示第一张图像,然后开始加载图像 在后台按顺序 2、3、4...
  2. 如果用户决定切换到图像 n(可以通过 搜索或拇指或...)并且触发的预加载(从1.)尚未加载n,在1.中建立的顺序(2,3,4,...)将被重新组织,以n为第一元素。

2. 的其他可能性可能是暂停预加载,加载图像 n,然后继续预加载,但我更喜欢第一个想法,因为这将为您提供更复杂的重新排序的自由,例如 (n, n+1, n+ 2,...,最后,第一个未加载,第二个未加载,...)。

到目前为止,我只是做了一个简单的预加载循环,如下所示:

for (var i = 0; i < issue.pages.length; i++)
{
  log("preloading image from URL " + issue.pages[i].imageUrl);
  var img = new Image();
  img.onload = function()
  {
    log("reading loading image " + this.src);
  };
  img.src = issue.pages[i].imageUrl;
} 

效果很好,但似乎阻止了其他图像的下载:如果我

imageN = new Image();
imageN.src = issue.pages[n].imageUrl;

在循环图像仍在加载时调用其他地方(拇指上的 onClick),imageN 在循环中的顺序轮到之前不会被加载。

有什么想法吗?

For a sort of slideshow I'm trying to implement a clever image pre-loader, which should work as following:

  1. On load of slideshow first image is shown and images start loading
    in background in order 2, 3, 4, ...
  2. If user decides to switch to image n (which can be done via
    search or thumbs or ...) and the triggered preload (from 1.) didn't load n yet, the order (2, 3, 4, ...) established in 1. will be re-organized with n as first element.

Other possibility for 2. could be to suspend preloading, load image n, and continue preload, but I'd prefer first idea because this will give you the freedom of more sophisticated re-ordering, e.g. (n, n+1, n+2, ..., last, 1st-not-loaded, 2nd-not-loaded, ...).

So far I just did a simple preload-loop like that:

for (var i = 0; i < issue.pages.length; i++)
{
  log("preloading image from URL " + issue.pages[i].imageUrl);
  var img = new Image();
  img.onload = function()
  {
    log("reading loading image " + this.src);
  };
  img.src = issue.pages[i].imageUrl;
} 

which works fine, but seems to block downloading of other images: If I call

imageN = new Image();
imageN.src = issue.pages[n].imageUrl;

somewhere else (onClick of thumb) while images of loop still loading, imageN will not be loaded before it's its turn from order in loop.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2024-12-05 19:22:56

据我所知,没有办法用 JavaScript 来停止或暂停图像加载。然而,控制图像加载顺序的一个巧妙方法是将它们的加载事件链接在一起。这是示例(使用 jQuery)。

在示例中,我有一个用作队列的数组 imgQueue 和一个函数 loadNextImage,该函数从队列中抓取图像进行加载并调用 loadNextImage code> 当图像加载完成时。

function loadNextImage() {
    if (imgQueue.length > 0) {
        var imgN = new Image();
        imgN.src = imgQueue.shift();
        $(imgN).load(function(){
            $("#main").append(imgN);
            loadNextImage();
        });
    }
}

要更改图像加载的顺序,您实际上只需将它们在 imgQueue 中向前移动,例如,如果我想将图像 5 移动到前面:

imgQueue.unshift(imgQueue.splice(4,1));

例如,这并不完美,除非图像是大的话,将它们分组加载可能更有意义。另外,由于 loadNextImage 操纵队列,因此您无法仅通过图像的原始索引来跟踪图像,因此您需要做一些奇特的事情,例如将它们存储为散列,以便以后可以找到它们:

var imgHashQueue = [
   {name: "name", url = "url"},
   ...
]

To my knowledge there is no way to stop or suspend an image loading with javascript. However, a neat way to control the order of the images loading would be to chain together their load events. Here is an example (using jQuery).

In the example I have an array imgQueue that I am using as a queue and a function loadNextImage that grabs an image off the queue to load and calls loadNextImage when the image is done loading.

function loadNextImage() {
    if (imgQueue.length > 0) {
        var imgN = new Image();
        imgN.src = imgQueue.shift();
        $(imgN).load(function(){
            $("#main").append(imgN);
            loadNextImage();
        });
    }
}

To change the order images are loading you would literally just move them ahead in imgQueue, for example if I wanted to move image 5 to the front:

imgQueue.unshift(imgQueue.splice(4,1));

This isn't perfect, for instance, unless the images are large it probably makes more sense to load them in groups. Also because loadNextImage manipulates the queue you can't keep track of images just by their original index, you'd need to do something fancy like store them as hashes so you can find them later:

var imgHashQueue = [
   {name: "name", url = "url"},
   ...
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文