我该怎么做才能让 Mozilla Firefox 预加载最终的图像结果?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议将
ul
与包含相关图像的子li
元素一起使用。使用display: none
设置ul
样式(或者,使用position:absolute
离开页面),并且图像仍应由浏览器,无论它们本质上对用户来说是不可见的。JS Fiddle 中说明了使用当前代码的一种稍微混乱的方法,相关的 JS下面:
显然上面并没有隐藏创建的
ul
,但是演示展示了动态创建它的一种方法。不可否认的是,到目前为止我只在 Chrome 中测试过它。I'd suggest using a
ul
with childli
elements that contain the relevant images. Style theul
withdisplay: 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:
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.为什么不在没有来源的页面上放置一个图像,然后隐藏它:
现在,在 Javascript 中,加载图像:
然后您可以稍后将此 img 放入 div 内。
Why don't you put an image on the page with no source, and hide it:
Now, in your Javascript, load the image:
Then you can place this img inside the div later.
我觉得你在这个问题上是站错了一边的~无论你用什么方法,最后都可能会被图主反驳。
为什么不将副本保存到您控制的主机上?
也就是说,您可以尝试
这可能会让 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
That may get FireFox to cache the image.