如何使用 jquery 判断 gif 是否已完全动画化

发布于 2025-01-06 05:52:54 字数 97 浏览 2 评论 0原文

如果我有一个仅运行单个动画的 IE 动画 gif。它不循环,有没有办法使用 jquery 或任何与此相关的东西来告诉动画何时完成?我不想告诉文件是否已完全加载,但动画已经运行完毕。

If I have an animated gif that just runs a single animation, IE. it doesn't loop, is there a way using jquery, or anything for that matter, of telling when the animation is complete? I don't want to tell if the file if fully loaded, but that the animation has run its course.

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

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

发布评论

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

评论(3

闻呓 2025-01-13 05:52:54

我认为没有办法从 JavaScript 中检测到它。我会选择不同的解决方案。

创建一个静态图像(可以是任何格式,JPEG、PNG、GIF),每个图像下方都有原始动画的帧
其他。像这样:

┌───┐
│ 1 │
├───┤
│ 2 │
├───┤
│ 3 │
├───┤
│ 4 │
└───┘

然后您可以创建一个

例如,具有帧的尺寸,并将这个长静态图像设置为背景,然后每隔 n 毫秒移动一次背景。

测试代码

$(function() {
  var width = 20, height = 20, frames = 3;

  var $div = $(document.createElement('div'))
    .css({
      backgroundImage: 'url(test.png)',
      width:  width + 'px',
      height: height + 'px'
    })
    .appendTo(document.body);

  var frame = 0;
  setInterval(function () {
    frame++;

    if (frame > frames) { frame = 0; }
    $div.css('background-position', '0 ' + -1 * frame * height + 'px');
  }, 250);
});

更新

Google 对 +1 按钮执行了相同的操作,但他们是水平执行的,而不是垂直执行的,如前面的示例: 在这里查看

I think there is no way to detect it from javascript. I would choose a different solution instead.

Create a static image (can be in any format, JPEG, PNG, GIF), which has the frames of the original animation below each
other. Like this:

┌───┐
│ 1 │
├───┤
│ 2 │
├───┤
│ 3 │
├───┤
│ 4 │
└───┘

Then you can create a <div/> for example with the dimensions of a frame and set this long static image as a background, then move the background in every nth milliseconds.

Tested code

$(function() {
  var width = 20, height = 20, frames = 3;

  var $div = $(document.createElement('div'))
    .css({
      backgroundImage: 'url(test.png)',
      width:  width + 'px',
      height: height + 'px'
    })
    .appendTo(document.body);

  var frame = 0;
  setInterval(function () {
    frame++;

    if (frame > frames) { frame = 0; }
    $div.css('background-position', '0 ' + -1 * frame * height + 'px');
  }, 250);
});

Update

Google did the same with the +1 buttons, but they did it in horizontally, not vertically, like the previous example: check it here.

萌逼全场 2025-01-13 05:52:54

你不能。 GIF 没有事件处理程序。

You can't. GIFs do not have event handlers.

若水微香 2025-01-13 05:52:54

答案很复杂,但确实有可能,但是您将需要一种服务器端语言,例如 PHP(除非您知道 GIF 将始终相同并且具有相同的已知持续时间)。

这个想法是您读取 GIF 文件的内容,因为动画 GIF 被分成多个帧,每个帧都有自己的标题。读取这些标头以获取帧数以及每帧之间的时间。

然后,您可以算出动画的持续时间(帧数 x FPS)。使用 jQuery 预加载 GIF,当它完全加载时显示它,然后等待它的动画持续时间。

The answer is complex but it is indeed possible, however you will need a server-side language such as PHP (unless you know that the GIF will always be the same and have the same known duration).

The idea is that you read the contents of the GIF file, as an animated GIF is broken into frames that each have its own header. Read these headers to get the number of frames and the time between each frame.

You can then work out the duration of the animation (No. of frames x FPS). Using jQuery preload the GIF and when it is fully loaded display it and then wait for the duration of it's animation.

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