简体   繁体   中英

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. This includes images though it seems it doesn't include GIF-images. From what I can see, loading the first frame of the gif counts as "loaded".

Is there a way to trigger code after the fill gif has been loaded?

The <img> element has on onload event - this should do the trick.

If you'd like to use jQuery for that, you can read about it here: https://api.jquery.com/load-event/

Images have a onload event that fires.

$(".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.

Edit: Went looking for the property name and found this question: jQuery callback on image load (even when the image is cached)

您可以将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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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