检测对象是否已加载 [Javascript]

发布于 2024-09-02 21:34:46 字数 98 浏览 2 评论 0原文

我想知道,有没有办法检测是否加载了某个图像/div? 例如,当我加载两个沉重的图像并在图像稍后占据的两个位置显示加载标志时,有没有办法在加载时显示第一个图像,同时仍在加载第二个图像?

I was wondering, is there a way to detect if a certain image / div is loaded?
For example when i am loading two heavy images and showing a loading sign at the two places the images will later occupy, is there a way to already display the first image when it's loaded while still loading the second one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮倦 2024-09-09 21:34:46
myImage.addEventListener('load', function() { ... }, false);

当图像加载完成时,将调用上述函数内的代码。

myImage.addEventListener('load', function() { ... }, false);

Code inside the above function will be called when the image is finished loading.

野稚 2024-09-09 21:34:46

如果您使用new Image来预加载图像,那么您可以执行以下操作以在加载后收到通知

var img = new Image();
img.onload = function() {
    //display the image
    document.getElementById("myDiv").innerHTML = "%3Cimg src='myimg.jpg' alt=''/%3E";
};
img.src = "myimg.jpg";

记住设置src after onload

If you are using new Image to preload images, then you can do the following to be notified of then it is loaded

var img = new Image();
img.onload = function() {
    //display the image
    document.getElementById("myDiv").innerHTML = "%3Cimg src='myimg.jpg' alt=''/%3E";
};
img.src = "myimg.jpg";

Remember to set the src after the onload.

说好的呢 2024-09-09 21:34:46

如果图像加载完成,其 .complete 属性将切换为 true。

if an image is done loading, its .complete property switches to true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文