4

I wana make the below gif which stoped initially but starts playing on hover and when mouseout it will stops... can anyone help me out??

enter image description here

stdob--
  • 28,222
  • 5
  • 58
  • 73
RJ Style
  • 111
  • 1
  • 2
  • 11

5 Answers5

5

In your case, cause animation is not complicated, ,my idea is to place two images on a page (animated and not). And show/hide them on mouse over/out.

<div id="img_wrap" class="static">
    <img id="animated" src="animated.gif" alt="">
    <img id="static" src="static.jpg" alt="">
</div>

Script:

$(function(){
    $('#img_wrap').on( 'mouseenter', function() {
         $(this).toggleClass('animated', 'static');
    })
})

CSS:

.animated #static, .static #animated {
    display: none;
}
.animated #animated, .static #static {
    display: inline;
}

Or you can do it even with a plain CSS, if you don't need a support for IE6, wich does not triggers hover event on anything but <a>:

CSS:

#img_wrap #static, #img_wrap:hover #animated {
    display: inline;
}
#img_wrap #animated, #img_wrap:hover #static {
    display: none;
}
fusion3k
  • 11,568
  • 4
  • 25
  • 47
d.k
  • 4,234
  • 2
  • 26
  • 38
  • Very elegant solution. The animation provided by the OP does not repeat forever. If it is an infinite animation, then it does repeat forever with this approach. This gave me an idea to think of other approaches!! – IcyFlame Mar 25 '14 at 06:15
1

do you need to use jquery here?

the gif doesn't load, but .div { background: url('.png'); } .div:hover { background: url('.gif'); }

lkiernan
  • 49
  • 7
  • on `hover` it will stop after the 1st iteration - the GIF should loop infinitely in order to make this work – Zoltan Toth Aug 26 '12 at 11:55
  • @Zoltan: `hover` causes GIFs with an infinite repeat animation to stop after the first iteration? – Lèse majesté Aug 26 '12 at 12:02
  • 2
    @Lèsemajesté sure not :) it's just the GIF that the OP provided - not infinite (to me at least), runs 1 iteration only – Zoltan Toth Aug 26 '12 at 12:06
  • @Zoltan: Ah, yea. If he wants to use that animation, then he'll either need to restart the animation manually with JS, or preferably, just edit the GIF so it repeats infinitely. I know the old Adobe ImageReady could do this, but I'm not sure how you edit the animation loop with the current Creative Suite. – Lèse majesté Aug 26 '12 at 12:22
1

If you just want to show a fixed static image when it's not animating, then it's as simple as changing the image on hover (with CSS or JS).

But if you want to actually freeze the animation on the current frame on mouseout, then the only way to do that is by animating the image manually, e.g. with JS:

(function(){
  var imgDownload = $('#BtnDownload'), interval = 250;

  function startAnimation(img, interval, frameCount) {
    var src, prefix, ext, toId;
    if (frameCount) img.data('frames', frameCount);
    interval = interval || img.data('interval');
    src = img.attr('src').split('.');
    ext = src.pop();
    prefix = src.join('.');
    img.data('ext') || img.data('ext', ext);
    img.data('prefix') || img.data('prefix', prefix);
    restartAnimation(img, interval);
    img.hover(function() {
      restartAnimation(img, interval);
    });
    img.mouseout(function() {
      clearTimeout($(this).data('timeout-id'));
    });
  }
  function restartAnimation(img, interval) {
    todId = setTimeout(animate, interval, img);
    img.data('timeout-id', toId);
  }
  function animate(img) {
    var currentFrame, nextFrame, frameCount, prefix, ext;
    currentFrame = img.data('current-frame');
    frameCount = img.data('frames');
    prefix = img.data('prefix');
    ext = img.data('ext');

    nextFrame = currentFrame + 1;
    if (nextFrame >= frameCount) nextFrame = 0;
    img.data('current-frame', nextFrame);
    img.attr('src', prefix + (nextFrame? nextFrame : '') + '.' + ext);
  }

  startAnimation(imgDownload, interval);
)());

and the following HTML:

<img src="/img/btn_download.png" alt="Download" data-frames="6">

and these images:

/img/btn_download.png
/img/btn_download1.png
/img/btn_download2.png
/img/btn_download3.png
/img/btn_download4.png
/img/btn_download5.png

Note: This is a naive implementation. For production code, you'd want to preload the images or simply use spritemaps. But the basic concept is the same—manually animate the image/button, so that when you freeze the animation, it freezes on the current frame. The alternative is using something like jsgif, which uses XHR to download the GIF file, parses the binary data to extract individual frames, and then renders them using HTML5 Canvas.

Lèse majesté
  • 7,923
  • 2
  • 33
  • 44
0

No, you can't control the animation of the images.

You would need two versions of each iamge, one that is animated, and one that's not. On hover you can easily change from one image to another.

prat1kk
  • 408
  • 5
  • 12
0

Get two images, one png and one gif :

<div>
    <img class="static" src="https://webpage.com/image1.png">
    <img class="active" src="https://webpage.com/image2.gif">
</div>

Use the following css, and on hover png will be opaque and gif is visible.

.static {
  position:absolute;
  background: white;
}

.static:hover {
  opacity:0;
}
london_utku
  • 1,070
  • 2
  • 16
  • 36