简体   繁体   English

在gif完全加载jQuery之后运行javascript

[英]Run javascript after gif is fully loaded with jQuery

I know I can use 我知道我可以使用

$(".container").ready(function(){
  // Run after DOM loaded
});

to run code after the DOM has been fully loaded. 在DOM完全加载后运行代码。 This includes images though it seems it doesn't include GIF-images. 这包括图像虽然它似乎不包括GIF图像。 From what I can see, loading the first frame of the gif counts as "loaded". 从我所看到的,加载gif的第一帧计为“已加载”。

Is there a way to trigger code after the fill gif has been loaded? 有没有办法在加载gif后触发代码?

The <img> element has on onload event - this should do the trick. <img>元素有onload事件 - 这应该可以解决问题。

If you'd like to use jQuery for that, you can read about it here: https://api.jquery.com/load-event/ 如果您想使用jQuery,可以在这里阅读: https//api.jquery.com/load-event/

Images have a onload event that fires. 图像有一个启动的onload event

$(".image-selector").on("load", function(){
   // Run after the image has loaded.
});

If you are targetting an older version of IE (pre IE9) there are sometimes troubles if dynamically changing the src property and you'll also have to check the image.complete property. 如果您的目标是IE的旧版本(IE9之前版本),如果动态更改src属性,有时会遇到麻烦,您还必须检查image.complete属性。

Edit: Went looking for the property name and found this question: jQuery callback on image load (even when the image is cached) 编辑:找到属性名称并发现这个问题: 图像加载时的jQuery回调(即使图像被缓存)

您可以将imagesloaded插件用于此用例。

How about this 这个怎么样

$(document).ready(function(){
    $("gif selector").load(function(){
        alert("gift loaded.");
    });
});

Example: http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_event_load 示例: http//www.w3schools.com/jquery/tryit.asp?filename = tryjquery_event_load

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM