我正在$(window).Load()上褪色一个图像,它的工作原理非常好,只是当您在社交网络上共享时,社交网络脚本有时会出现问题,需要超过10秒的时间加载,这意味着在加载脚本之前,图像不会fadeIn!
是否有办法使以下内容忽略社会脚本?
$(window).load(function() {
images.fadeIn('slow');
});正在加载的社交脚本是显示“共享”按钮所需的,它们包括Facebook、Twitter、Pinterest、StumbleUpon和Google+。就在几秒钟前,"pinterest“脚本在加载时被卡住了,加载它的代码也有”异步“,所以我不知道我还能做什么?
发布于 2013-10-14 10:00:22
你有两种选择:
1-不要等window.load
与其等待窗口加载,不如寻找图像加载。例如,在HTML中的图像下面使用这个脚本(或者使用jQuery的ready事件):
(function() {
var imgs = $("selector for the images you care about");
var done = false;
// Hook both load and error events on the images
imgs.on("load error", check);
// We may well have missed some, so do a proactive check
check();
function check() {
var count = 0;
if (!done) {
imgs.each(function() {
if (this.complete) {
++count;
}
});
if (count === imgs.length) {
done = true;
imgs.off("load error");
// Do your effect
}
}
}
})();请注意,我们不依赖于获取load事件,因为它可能在我们连接它之前就已经启动了。因此,我们进行了一次初始检查,以防错过所有加载事件,然后当我们从任何相关图像中看到load或error时,再次检查。
2-异步加载相关脚本
不要在标记中使用<script>标记,而是通过将script元素附加到DOM中来添加您不想等待的脚本。以这种方式添加的脚本是异步加载的,并且不延迟window.load事件。
例如:
(function() {
var scripts = [
"http://example.com/some/script.js",
"http://example.com/some/another_script.js",
"http://example.com/some/script3.js"
};
var index, script;
var parent = document.getElementsByTagName("script")[0].parentNode;
for (index = 0; index < scripts.length; ++index) {
script = document.createElement("script");
script.src = scripts[index];
parent.appendChild(script);
}
})();https://stackoverflow.com/questions/19357571
复制相似问题