Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating a gif on hover

I've looked for the answer for this and I found it, but I don't know how to use it.

Stop a gif animation onload, on mouseover start the activation

Guffa's answer to that question is exactly what I want, but I don't know how to use that code.

I have the jquery plugin, but where do I put the code (not the plugin; the code that was in Guffa's answer)? How do I use it in reference to the images? Is there a function I have to call to get it to work? If so, what would be the best way to call it?

Sorry for asking a question that has already been answered, but his answer wasn't specific enough and I couldn't comment to ask him for a more specific answer.

like image 709
Mark Kramer Avatar asked Sep 19 '11 15:09

Mark Kramer


People also ask

What is a hover effect?

What is a CSS Hover Effect? A CSS hover effect takes place when a user hovers over an element, and the element responds with transition effects. It is used to mark the key items on the web page and it's an effective way to enhance the user experience.

What is CSS hover?

The :hover CSS pseudo-class matches when the user interacts with an element with a pointing device, but does not necessarily activate it. It is generally triggered when the user hovers over an element with the cursor (mouse pointer).


2 Answers

Here is a working example for what you need - http://jsfiddle.net/EXNZr/1/

<img id="imgDino" src="http://bestuff.com/images/images_of_stuff/64x64crop/t-rex-51807.jpg?1176587870" />

<script>
    $(function() {
        $("#imgDino").hover(
            function() {
                $(this).attr("src", "animated.gif");
            },
            function() {
                $(this).attr("src", "static.gif");
            }                         
        );                  
    });
</script>
like image 149
Amit Avatar answered Sep 18 '22 14:09

Amit


I haven't read the link, however the easiest way to do this is to change the img tags src attribute with javascript on hover like this (jQuery)

$(function() {
    $('a').hover(function() {
      $(this).attr('src','path_to_animated.gif');
    },function() {
      $(this).attr('src','path_to_still.gif');
    }
});

No plugins required... you might want to preload the animated gif by adding $('<img />',{ src: 'path_to_animated.gif'}); before the hover bind.

Hope that helps

like image 26
ianbarker Avatar answered Sep 17 '22 14:09

ianbarker