Skip to content

Instantly share code, notes, and snippets.

@coclav
Last active March 11, 2019 18:43
Show Gist options
  • Save coclav/5760690 to your computer and use it in GitHub Desktop.
Save coclav/5760690 to your computer and use it in GitHub Desktop.
Start the animation of a .gif on click. This adds a "start / stop" button below your still image (abc_still.gif / class : img_anim) that will change the .gif to the animated version (abc.gif)
jQuery(".img_anim").after( function() {
// keep ref to the image
var image = this;
// add div container for the link
var $d = jQuery("<div />");
// add link
var $a = jQuery("<a>Start / Stop animation</a>");
$d.append($a);
// add click event
$a.on("click", function() {
// get the src of the image
var src = jQuery(image).attr("src");
// change the image
if(jQuery(src.split("_")).last()[0] == "still.gif")
jQuery(image).attr('src', src.replace('_still.gif', '.gif'));
else
jQuery(image).attr('src', src.replace('.gif', '_still.gif'));
})
return $d;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment