简体   繁体   English

动画GIF背景仅显示一次

[英]Animated GIF background only showing once

So on click of these polaroid like images, I replace them with <div class="poof"></div> using jQuery's .replaceWith() . 因此,当点击这些宝丽来图像时,我使用jQuery的.replaceWith()将它们替换为<div class="poof"></div> The reason I do this is to display a poof animated gif animation that I style to that class with js. 我这样做的原因是为了显示我用js样式化的po动画gif动画。

However, on click of the .polaroid , the polaroid images, the .poof div replaces that html every time, but its background never shows. 然而, 在中点击.polaroid ,宝丽来图像, .poof DIV替换HTML每一次,但其背景永远不会显示。

Try it for yourself, here : 在这里自己尝试

Click an image, it is replaced by the .poof div, but the poof animation doesn't show. 单击图像,将其替换为.poof div,但不显示poof动画。

I would greatly appreciate it someone could help me figure out why this is and how to get the animation to show each time. 我将不胜感激,有人可以帮助我弄清楚这是为什么,以及如何使动画每次显示。

Here is my javascript: 这是我的JavaScript:

   $(function(){
        var time=1;
        $('.polaroid').click(function(){
            $(this).replaceWith('<div class="poof"></div>');
            $('.poof').css('height', $(this).height()).css('width', $(this).width()).css('background', 'transparent url(poof.gif?t='+time+') center no-repeat');
            time++;
        });
});

Here is .poof 's CSS: 这是.poof的CSS:

.poof{
    height:32px;
    width:32px;
 }

You are getting the height and width of an object that was remove / replaced in the DOM 您将获得在DOM中已删除/替换的对象的高度和宽度

Change to this: 更改为此:

var heights = 32;
var widths = 32;
$('.polaroid').click(function(){
    heights = $(this).height();
    widths = $(this).width();
    $('.poof').css('height', heights).css('width', widths).css('background', 'transparent url(http://i.imgur.com/FsVe2LC.gif?t='+time+') center no-repeat');
    time++;
    setTimeout(function(){
        $('.poof').remove();
    }, 1500);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM