Add dynamically a gif image with animation

Alvaro Joao

I'm trying to load programmatically an img with a .gif extension. But the animation only happens once.

How can I add more img and see the animation every time I add a new img, not just the final state?

Note: I do not want a loop gif. I just want the next img to behave just like the first, animate then stop.

var topval = 10;
var right = 10;

var addStar = function() {
	var img = $('<img id="'+topval+'">'); 
	img.attr('src','https://s3-sa-east-1.amazonaws.com/musicamise/astronautAnimatedGif.gif');
	img.attr('alt','alt'+topval);
	img.css('position','absolute');
	img.css('right', right+'px');
	img.css('top', topval+'px');
	img.appendTo('body');

	topval = topval+10;
	right = right+10;
};
$("#addStar").click(addStar);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body style="margin:0;padding:0;">
     <button id="addStar">Click do add</button>
</body>

Thanks in advance.

alexqoliveira

Your problem is because the browser takes the same cache image. When you change the URL of the image, it's like you're looking for another image. The downside of this is that he always makes a new request for the image.

var topval = 10;
var right = 10;

var count = 0;

var addStar = function(){
	var img = $('<img id="'+topval+'">'); 
	img.attr('src','https://s3-sa-east-1.amazonaws.com/musicamise/astronautAnimatedGif.gif?count='+count++ );
	img.attr('alt','alt'+topval);
	img.css('position','absolute');
	img.css('right', right+'px');
	img.css('top', topval+'px');
	img.appendTo('body');

	topval = topval+10;
	right = right+10;
}
$("#addStar").click(addStar);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body style="margin:0;padding:0;">
     <button id="addStar">Click do add</button>
</body>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rotate GIF Image And Save GIF Animation

From Dev

Add Image in tabbar (.gif)

From Dev

Add image above gif with ffmpeg

From Dev

How to Add GIf image as layer on view?

From Dev

Can you show a static image after some animation loops in a gif?

From Dev

Can you show a static image after some animation loops in a gif?

From Dev

How to dynamically add Image to dynamically created ImageView

From Dev

How to add Image to ImageView dynamically?

From Dev

Dynamically add image to a slider in wordpress

From Dev

Add element on top of image dynamically

From Dev

Add an image dynamically to this beautiful frame

From Dev

how to dynamically add image to imageView

From Dev

Add element on top of image dynamically

From Dev

Add a delay to a set image after animation

From Dev

Add GIF image to marker of Google Map Api v2

From Dev

Add GIF image to marker of Google Map Api v2

From Dev

WPF button gif animation

From Dev

Gif Animation in Gnuplot

From Dev

Controlling GIF animation with Glide

From Dev

resize gif animation in android

From Dev

WPF button gif animation

From Dev

How to add an image inside a dynamically created div?

From Dev

Add value to dynamically generated image then display on click

From Dev

How to add an image inside a dynamically created div?

From Dev

Need to add alt tag for image Dynamically in javascript

From Dev

Add value to dynamically generated image then display on click

From Dev

How to add left border with image(s) dynamically

From Dev

How to dynamically add elements to an SVG image with jquery?

From Dev

Dynamically add Image to ImageList that lasts in C#