简体   繁体   English

PreloadJS对于以后添加的元素无法正常工作(在Firefox中)

[英]PreloadJS doesn't work correctly for elements appended later (in Firefox)

I'm having a problem with how PreloadJS is behaving on Firefox. 我对PreloadJS在Firefox上的行为有疑问。 Actually it's hard for me to believe nobody else was having this problem before (couldn't find anyone describing same behavior) so perhaps I'm just doing something wrong... it works just fine in google chrome through. 实际上,我很难相信以前没有其他人遇到过这个问题(找不到任何人描述相同的行为),所以也许我只是做错了什么...它在谷歌浏览器中正常工作。

Here's JS part: 这是JS部分:

$(document).ready(function () {
  var preloadBG = new createjs.LoadQueue();
   preloadBG.addEventListener("fileload", function(){
     $('#light').css("background-color","green");
     $("#container").append('<div id="image"></div>');
   });
   preloadBG.loadFile('http://i.imgur.com/ifvZ5Ss.jpg');
});

See example here: http://codepen.io/Deto15/pen/KdpRdx 在此处查看示例: http : //codepen.io/Deto15/pen/KdpRdx

You'll notice the behavior I'm describing if you run it with Firefox and Ctrl+F5. 如果您使用Firefox和Ctrl + F5运行它,您会注意到我正在描述的行为。

So basically what I'm doing here is this: 所以基本上我在这里是这样的:

  1. On domready PreloadJS preloads image (it's not actually used anywhere on page yet. 在domready上,PreloadJS会预加载图像(页面上尚未实际使用它。
  2. On complete of preloading red circle turns to green. 预加载完成后,红色圆圈变为绿色。
  3. Right after that new div is appended to page, and that div uses image preloaded earlier as background-image. 在该新div追加到页面之后,该div使用先前预加载的图像作为背景图像。

The way it behaves in Chrome is that image appears at pretty much same moment as when the circle changes color - and that makes sense since it's been preloaded. 它在Chrome浏览器中的行为方式是,图片与圆圈更改颜色的时间几乎相同,这是有道理的,因为它已预先加载。 On Firefox through, the circle changes color and then there's a lag before image appears - as if Firefox was loading it again. 在Firefox上,圆圈会更改颜色,然后在图像出现之前会有一个延迟-就像Firefox在重新加载一样。

Is there any explanation to this behavior and any way to fix this? 是否对此行为有任何解释以及解决此问题的任何方法?

Comparing the two in dev tools: 比较开发工具中的两个:

Firefox和Chrome比较

From what I can see, Firefox is not retrieving the image from cache while Chrome does. 据我所知,Firefox无法从缓存中检索图像,而Chrome却可以。

One possible solution could be related to the image size, as detailed here . 一个可能的解决方案可能与图像大小,详见这里

I'd suggest testing with smaller images first to see if that resolves the caching issue. 我建议先使用较小的图像进行测试,看看是否可以解决缓存问题。

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

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