插入CSS后如何使用jQuery检测加载的背景图像 [英] How to use jQuery to detect loaded background image after inserting out CSS

查看:74
本文介绍了插入CSS后如何使用jQuery检测加载的背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Google上搜索了很长时间以找到解决方案,但最终还是失败了.

I had googled for a long time to find out the solution but failed in the end.

以下是我的详细问题:

我有一个用jQuery lib编写的javascript代码.

I have a javascript code written with jQuery lib.

$('<link id="userCSS" rel="stylesheet" href="'+link+'" type="text/css" charset="utf-8">').insertAfter("title");

您会发现我们将从其他地方动态加载CSS文件.

You can find out that we will dynamically load the CSS file from somewhere else.

然后,由于某人的服务器生成的文件因人而异,因此可能会有一个随机的css段,如下所示:

And then , because the file generated by someone's server is different from person to person, there may have a random css segment like this:

body,html {
  background:#000 url(http://i43.tinypic.com/xxxx.jpg) top left no-repeat;
  font-family:Arial,Verdana,sans-serif;
  font-size:12px;
}

所以,这里出了问题!我希望可以在加载背景图片时触发onload事件,但是我已经尝试了所有方法,但没有希望.

So , here comes a problem ! I hope I can trigger the onload event when background image is loaded but I had tried all ways with no hope.

有人在此方面有相关经验吗?谢谢:]

Does anyone have some related experiences with this and give me a hand !? Thanks :]

推荐答案

没有示例代码,但我建议的最好方法是提取括号中的完整URL,然后创建一个图像对象,将源设置为该URL,然后然后将您想要执行的任何操作绑定到该图像的onload.我不是100%会用,但我相信对于背景和您的新图像对象,该图像只会下载一次,并在正确的时间触发该事件.

No example code, but the best I can suggest is to extract the full url in parentheses, then create an image object, set the source to that url, and then bind whatever you action you want to the onload for that image. I'm not 100% it will work, but I believe that the image will only be downloaded once for both the background and your new image object, firing the event at the correct time.

您可能还需要使用 event.special.load插件,以防后台图像在创建图像对象之前被下载.因为如果发生这种情况,它将来自onload事件可能会或可能不会正确处理的缓存.

You may need to also use the event.special.load plugin in case the image in the background gets downloaded before the image object is created. Because if that happens, it'll be coming from the cache which the onload event may or may not handle correctly.

这篇关于插入CSS后如何使用jQuery检测加载的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆