【问题标题】:Run javascript after gif is fully loaded with jQuery用 jQuery 完全加载 gif 后运行 javascript
【发布时间】:2016-10-11 12:01:47
【问题描述】:

我知道我可以使用

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

在 DOM 完全加载后运行代码。这包括图像,但它似乎不包括 GIF 图像。据我所知,加载 gif 的第一帧算作“加载”。

有没有办法在填充gif加载后触发代码?

【问题讨论】:

  • 如我所说,我知道“就绪”功能。它似乎不包括 gifs

标签: javascript jquery gif onload


【解决方案1】:

<img> 元素在 onload 事件上具有 - 这应该可以解决问题。

如果您想为此使用 jQuery,可以在此处阅读:https://api.jquery.com/load-event/

【讨论】:

    【解决方案2】:

    图像有一个onload event 会触发。

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

    如果您的目标是旧版本的 IE(IE9 之前),动态更改 src 属性有时会出现问题,并且您还必须检查 image.complete 属性。

    编辑:去寻找属性名称,发现这个问题:jQuery callback on image load (even when the image is cached)

    【讨论】:

      【解决方案3】:

      您可以为此用例使用imagesloaded 插件。

      【讨论】:

        【解决方案4】:

        这个怎么样

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-02
          • 2023-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多