JavaScript:了解图像何时完全加载 [英] JavaScript: Know when an image is fully loaded

查看:108
本文介绍了JavaScript:了解图像何时完全加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个灯塔:

<img src="http://example.com/beacon" />

我希望在信标请求完成后调用一个方法。类似于:

I want a method to be called once the beacon request finishes. Something like:

<script>
    $("img.beacon").load(function() {
        // do stuff knowing the beacon is done
    });
</script>

有可能吗?它是在jQuery中吗?

Is it possible? Is it in jQuery?

推荐答案

当然。请记住,需要在src属性之前添加负载。

Sure. Remember the load needs to be added before the src attribute.

$('<img />').load( function(){
  console.log('loaded');
}).attr('src', imgUrl);

如果你在标记中定义了图像标记,那么当窗口加载事件触发时你就会卡住确保图像已经下线。

If you have defined the image tag in the markup then your stuck with when the window load event fires to be sure the image has come down the wire.

这篇关于JavaScript:了解图像何时完全加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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