我该怎么做才能让 Mozilla Firefox 预加载最终的图像结果?

发布于 2024-10-19 12:52:18 字数 1077 浏览 2 评论 0原文

我正在尝试使用 JavaScript 预加载图像。我已声明一个数组,如下所示,其中包含来自不同位置的图像链接:

var imageArray = new Array();
imageArray[0] = new Image();
imageArray[1] = new Image();
imageArray[2] = new Image();
imageArray[3] = new Image();
imageArray[0].src = "http://www.bollywoodhott.com/wp-content/uploads/2008/12/arjun-rampal.jpg";
imageArray[1].src = "http://labelleetleblog.files.wordpress.com/2009/06/josie-maran.jpg";
imageArray[2].src = "http://1.bp.blogspot.com/_22EXDJCJp3s/SxbIcZHTHTI/AAAAAAAAIXc/fkaDiOKjd-I/s400/black-male-model.jpg";
imageArray[3].src = "http://www.iill.net/wp-content/uploads/images/hot-chick.jpg";

我使用此数组执行的图像淡入淡出和变换效果对于前 3 个图像正常工作,但对于最后一个图像,imageArray[3],图像的实际图像数据没有被预加载,它完全破坏了效果,因为实际的图像数据是在之后加载的,似乎只在需要显示的时候加载。

发生这种情况是因为最后一个链接 http://www.iill.net/wp-content/uploads/images/hot-chick.jpg 不是图像的直接链接。如果您访问该链接,您的浏览器会将您重定向到实际位置。现在,我在 Chrome 中的图像预加载代码运行得很好,而且效果看起来很棒。因为 Chrome 似乎预加载了实际数据 - 要显示的事件图像。这意味着在 Chrome 中,如果我预加载的图像将重定向到“停止窃取我的带宽”,那么预加载的图像就是“停止窃取我的带宽”。

我如何修改我的代码以使 Firefox 表现出相同的行为?

I am attempting to preload images using JavaScript. I have declared an array as follows with image links from different places:

var imageArray = new Array();
imageArray[0] = new Image();
imageArray[1] = new Image();
imageArray[2] = new Image();
imageArray[3] = new Image();
imageArray[0].src = "http://www.bollywoodhott.com/wp-content/uploads/2008/12/arjun-rampal.jpg";
imageArray[1].src = "http://labelleetleblog.files.wordpress.com/2009/06/josie-maran.jpg";
imageArray[2].src = "http://1.bp.blogspot.com/_22EXDJCJp3s/SxbIcZHTHTI/AAAAAAAAIXc/fkaDiOKjd-I/s400/black-male-model.jpg";
imageArray[3].src = "http://www.iill.net/wp-content/uploads/images/hot-chick.jpg";

The image fade and transformation effects that I am doing using this array work properly for the first 3 images, but for the last one, imageArray[3], the actual image data of the image does not get preloaded and it completely ruins the effect, since the actual image data loads AFTERWARDS, only at the time it needs to be displayed, it seems.

This happens because the last link http://www.iill.net/wp-content/uploads/images/hot-chick.jpg is not a direct link to the image. If you go to that link, your browser will redirect you to the ACTUAL location. Now, my image preloading code in Chrome works perfectly well, and the effects look great. Because it seems that Chrome preloads the actual data - the EVENTUAL image that is to be shown. This means that in Chrome if I preloaded an image that will redirect to 'stop stealing my bandwidth', then the image that gets preloaded is 'stop stealing my bandwidth'.

How can I modify my code to get Firefox to behave the same way?

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

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

发布评论

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

评论(3

¢好甜 2024-10-26 12:52:18

我建议将 ul 与包含相关图像的子 li 元素一起使用。使用 display: none 设置 ul 样式(或者,使用 position:absolute 离开页面),并且图像仍应由浏览器,无论它们本质上对用户来说是不可见的。

JS Fiddle 中说明了使用当前代码的一种稍微混乱的方法,相关的 JS下面:

var imageArray = new Array();
imageArray[0] = new Image();
imageArray[1] = new Image();
imageArray[2] = new Image();
imageArray[3] = new Image();

imageArray[0].src = "http://www.bollywoodhott.com/wp-content/uploads/2008/12/arjun-rampal.jpg";
imageArray[1].src = "http://labelleetleblog.files.wordpress.com/2009/06/josie-maran.jpg";
imageArray[2].src = "http://1.bp.blogspot.com/_22EXDJCJp3s/SxbIcZHTHTI/AAAAAAAAIXc/fkaDiOKjd-I/s400/black-male-model.jpg";
imageArray[3].src = "http://www.iill.net/wp-content/uploads/images/hot-chick.jpg";

// the below stuff is the new:

var newList = document.createElement('ul');
newList.setAttribute('id', 'imgPreload');

var preload = document.getElementById('preload');
preload.appendChild(newList);
var imgPreload = document.getElementById('imgPreload');

var li = document.createElement('li');

for(i=0; i<imageArray.length; i++){
    imgPreload.appendChild(li).appendChild(imageArray[i]);
}

显然上面并没有隐藏创建的ul,但是演示展示了动态创建它的一种方法。不可否认的是,到目前为止我只在 Chrome 中测试过它。

I'd suggest using a ul with child li elements that contain the relevant images. Style the ul with display: none (or, alternatively, position: absolute it off the page) and the images should still be loaded by the browser, regardless of their being, essentially, invisible to the user.

A slightly messy way of doing that, with your current code, is illustrated at JS Fiddle, the relevant JS below:

var imageArray = new Array();
imageArray[0] = new Image();
imageArray[1] = new Image();
imageArray[2] = new Image();
imageArray[3] = new Image();

imageArray[0].src = "http://www.bollywoodhott.com/wp-content/uploads/2008/12/arjun-rampal.jpg";
imageArray[1].src = "http://labelleetleblog.files.wordpress.com/2009/06/josie-maran.jpg";
imageArray[2].src = "http://1.bp.blogspot.com/_22EXDJCJp3s/SxbIcZHTHTI/AAAAAAAAIXc/fkaDiOKjd-I/s400/black-male-model.jpg";
imageArray[3].src = "http://www.iill.net/wp-content/uploads/images/hot-chick.jpg";

// the below stuff is the new:

var newList = document.createElement('ul');
newList.setAttribute('id', 'imgPreload');

var preload = document.getElementById('preload');
preload.appendChild(newList);
var imgPreload = document.getElementById('imgPreload');

var li = document.createElement('li');

for(i=0; i<imageArray.length; i++){
    imgPreload.appendChild(li).appendChild(imageArray[i]);
}

Obviously the above doesn't hide the created ul, but the demo's to show one way of going about creating it dynamically. Admittedly, also, I've only so far tested it in Chrome.

蒗幽 2024-10-26 12:52:18

为什么不在没有来源的页面上放置一个图像,然后隐藏它:

<img src='' id='preLoadImage1' style='display: none' />

现在,在 Javascript 中,加载图像:

document.getElementById('preLoadImage1').src = '';

然后您可以稍后将此 img 放入 div 内。

Why don't you put an image on the page with no source, and hide it:

<img src='' id='preLoadImage1' style='display: none' />

Now, in your Javascript, load the image:

document.getElementById('preLoadImage1').src = '';

Then you can place this img inside the div later.

天冷不及心凉 2024-10-26 12:52:18

我觉得你在这个问题上是站错了一边的~无论你用什么方法,最后都可能会被图主反驳。
为什么不将副本保存到您控制的主机上?

也就是说,您可以尝试

<img src="hotlink" style="visibility:hidden;position:absolute;margin-top:-1000px;" />

这可能会让 FireFox 缓存图像。

I think you are on the wrong side of this issue~ Whatever method you employ may eventually be countered by the host of the image.
Why not save a copy to a host you control?

That said, you could try

<img src="hotlink" style="visibility:hidden;position:absolute;margin-top:-1000px;" />

That may get FireFox to cache the image.

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