×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Matthew Speicher
Added: Jun 24, 2016 2:22 PM
Views: 7
Tags: no tags
  1. <img id="img1" src="static_img" onclick="activateGIF(this, '#anim', 200)"/> <!-- static img -->
  2. <img id="anim" src="anim_img"/> <!-- Animation -->
  3. <script type="text/javascript">
  4. function activateGIF(id, anim, timeout) {
  5.     var animsrc = $(anim).attr('src'); //Get anim src
  6.     var src = $(id).attr('src'); //Get static img src
  7.     var onclick = $(id).attr('onclick'); //Get onclick
  8.     $(id).attr('src', animsrc).removeAttr('onclick').show(); //Attribute onclick should be removed, 'cause this function gets fired every time a clickevent gets fired
  9.     setTimeout(function(){$(id).attr('src', src).attr('width', '666').attr('height', '583').attr('onclick', onclick);}, timeout); //sets timeout, so animation 'stops'
  10. }
  11. </script>
  12.