gpt4 book ai didi

javascript - 如果没有 setTimeout,ImagesLoaded 无法在 Firefox 中使用

转载 作者:行者123 更新时间:2023-11-28 01:06:29 26 4
gpt4 key购买 nike

您知道为什么我需要至少 300 毫秒的超时才能让 imagesLoaded 在 Firefox 上运行吗?如果我将其设置得更少,则 imagesLoaded 事件会在图像加载之前立即触发。我使用的是最新版本3.1.x。

<html>
<script src="bower_components/jquery/jquery.min.js"></script>
<script src="bower_components/imagesloaded/imagesloaded.pkgd.js"></script>
<head>
</head>
<body>
<div id=myimage><img style="height:100px" src="http://www.worldcarwallpapers.com/wp-content/uploads/2013/07/new-2014-audi-r8-wallpaper.jpg"/></div>
<script>
$(document).ready(function(){
setTimeout(function(){
var elem = $("#myimage");
elem.imagesLoaded(function(){
alert("ready!!")
});
}, 100)
})
</script>
</body>
</html>

最佳答案

在该库的文档中,作者以不同的方式使用它( here )。问题可能是当库启动时,可能是在页面加载时,它不清楚。我认为您不应该为一张图像使用库。您可以将其添加到您的头脑中

<script>
HTMLImageElement.prototype.imageLoaded = function(callback){
if(this.complete && this.naturalWidth>0){
callback();
}else{
this.addEventListener('load', callback, false);
}
};
</script>

然后在 body 负载上附加您的事件,如下所示:

$(function(){
var elem = document.getElementById("myimage");
elem.imageLoaded(function(){
alert("ready!!")
});
});

这不适用于图像加载错误。

关于javascript - 如果没有 setTimeout,ImagesLoaded 无法在 Firefox 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25021690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com