如何使用JS触发GIF动画?

Isa*_*bow 6 javascript jquery gif animated-gif

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

 $('#plus').click(function(){
    $('#plus').attr('src','');
    $('#plus').attr('src','img/plus.gif')
 });
Run Code Online (Sandbox Code Playgroud)

希望快速重置src会触发动画,但没有运气.谁知道会怎么做?

ada*_*101 8

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

<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>
Run Code Online (Sandbox Code Playgroud)

  • 时间戳优于随机生成.另请看这里==> http://stackoverflow.com/questions/728616/disable-cache-for-some-images (5认同)
  • 谢谢大佬,你搞定了。但我认为我的方法更简单:`$('#plus').attr('src','img/plus.gif?x='+Math.round(Math.random()*1000));` (2认同)