如何使用JS触发GIF动画?

发布于 2024-09-28 11:28:52 字数 229 浏览 5 评论 0原文

我有一个播放一次的动画 GIF(不循环)。我希望它在点击时有动画效果。 我尝试了这样的事情:

 $('#plus').click(function(){
    $('#plus').attr('src','');
    $('#plus').attr('src','img/plus.gif')
 });

希望快速重置 src 会触发动画,但没有运气。有人知道会做什么吗?

I have an animated GIF that plays once (doesn't loop). I would like it to animate when clicked.
I tried something like this:

 $('#plus').click(function(){
    $('#plus').attr('src','');
    $('#plus').attr('src','img/plus.gif')
 });

With the hope that quickly resetting the src would trigger the animation, but no luck. Anyone know what would do it?

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

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

发布评论

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

评论(6

度的依靠╰つ 2024-10-05 11:28:52

尝试使用随机生成的查询字符串添加图像,使其在浏览器中看起来像新图像。

<script language="javascript" type="text/javascript">
function randomString() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
    document.randform.randomfield.value = randomstring;
}
$('#plus').click(function(){
    $('#plus').attr('src','img/plus.gif?x=' + randomString())
    });
</script>

Try adding the image with a randomly generated querystring so it looks like a new image to the browser.

<script language="javascript" type="text/javascript">
function randomString() {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var string_length = 8;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
    document.randform.randomfield.value = randomstring;
}
$('#plus').click(function(){
    $('#plus').attr('src','img/plus.gif?x=' + randomString())
    });
</script>
不语却知心 2024-10-05 11:28:52
function refreshSrc(who){
    return who.src= who.src.split('?')[0]+'?='+(+new Date());
}
refreshSrc(document.images[0])

如果您愿意,可以使用 jQuery 语法来启动它。

function refreshSrc(who){
    return who.src= who.src.split('?')[0]+'?='+(+new Date());
}
refreshSrc(document.images[0])

Tart it up with jQuery syntax, if you like.

青巷忧颜 2024-10-05 11:28:52

这是一种替代方法。

您可以将各个帧导出为其自己的图像并通过 JavaScript 处理动画。

它可以让你做一些很酷的事情。一位同事最近制作了一个小计时器动画,该动画与我们图片库中的可配置幻灯片间隔同步。

您从中得到的另一件事是您可以使用 png 并具有半透明的背景。

某处可能有一个用于 javascript 的精灵动画库:)

This is a bit of an alternative approach.

You could export the individual frames as their own images and handle animation via javascript.

It lets you do a couple of cool things. A colleague recently had a little timer animation that was synced to the configurable slideshow interval in our image gallery.

The other thing you get out of this is you could use pngs and have translucent backgrounds.

There's probably a sprite animation library for javascript out there somewhere :)

何其悲哀 2024-10-05 11:28:52

您是否尝试过删除 img 标签并再次添加回来? (没试过,只是一个想法)

Have you tried removing the img tag and adding it back again? (never tried it, just an idea)

舂唻埖巳落 2024-10-05 11:28:52

您可以尝试使用单帧图像(动画的第一帧)并在单击时显示/隐藏每个图像。如果你想让它在动画完成后重置回单帧图像,你可以使用setTimeout(以动画的长度作为时间的长度)并让它隐藏动画并显示静态图像。

You could try having a single-frame image (of the first frame of animation) and show/hide each one on click. If you want it to reset back to the single-frame image when the animation is done, you could use setTimeout (with how long the animation is as the length of time) and have it hide the animation and show the static image.

魂牵梦绕锁你心扉 2024-10-05 11:28:52

对于那些想要在滚动到视图中时执行此操作的人,我执行了以下操作。

override.js 的链接: https://github.com/morr/jquery.appear

$('img.large-magnify-glass-animation').appear(function() {
    $(this).delay(200, function(){
        $(this).attr('src','http://example.com/path/to/gif.gif');
    });
});

我只是在短暂的延迟后,通过 attr('src') 在可见性上加载相同的 gif。没有时间戳或随机字符函数。刚刚使用了出现插件。

For those who want to do this when it scrolls into view i did the following.

Link for appear.js : https://github.com/morr/jquery.appear

$('img.large-magnify-glass-animation').appear(function() {
    $(this).delay(200, function(){
        $(this).attr('src','http://example.com/path/to/gif.gif');
    });
});

I just loaded the same gif via the attr('src') upon visiblity after a short delay. No timestamp or random char function. Just used appear plugin.

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