如何使用JS触发GIF动画?
我有一个播放一次的动画 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试使用随机生成的查询字符串添加图像,使其在浏览器中看起来像新图像。
Try adding the image with a randomly generated querystring so it looks like a new image to the browser.
如果您愿意,可以使用 jQuery 语法来启动它。
Tart it up with jQuery syntax, if you like.
这是一种替代方法。
您可以将各个帧导出为其自己的图像并通过 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 :)
您是否尝试过删除 img 标签并再次添加回来? (没试过,只是一个想法)
Have you tried removing the img tag and adding it back again? (never tried it, just an idea)
您可以尝试使用单帧图像(动画的第一帧)并在单击时显示/隐藏每个图像。如果你想让它在动画完成后重置回单帧图像,你可以使用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.
对于那些想要在滚动到视图中时执行此操作的人,我执行了以下操作。
override.js 的链接: https://github.com/morr/jquery.appear
我只是在短暂的延迟后,通过 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
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.