Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery hover mouse out

Tags:

jquery

hover

I want a simple slide down / up animation on a mouse over of a link. I can get the mouse over to work but I can't work out how to get the mouseout to do it's thing.

Here's what I have for the hover effect:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>

<script type="text/javascript">

google.load("jquery", "1.3.2"); //load version 1.3.2 of jQuery

google.setOnLoadCallback(function() {
    jQuery(
        function($) {
            $("a.button").hover(function(){$(this).animate({"marginTop": "0px"}, "fast")   

        });
    });
});
</script>

How do I get this to move the margin up 16px when mouse out?

like image 359
Stuart Robson Avatar asked Jul 12 '09 16:07

Stuart Robson


People also ask

What is Mouseout in jQuery?

jQuery mouseout() Method The mouseout event occurs when the mouse pointer leaves the selected element. The mouseout() method triggers the mouseout event, or attaches a function to run when a mouseout event occurs.

Is hover deprecated in jQuery?

hover() is deprecated #66.

What is the jQuery equivalent of onmouseover?

jQuery mouseover() Method Note: Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element. The mouseenter event is only triggered when the mouse pointer enters the selected element. See the example at the end of the page for a demonstration.

How can write hover function in jQuery?

The hover() is an inbuilt method in jQuery which is used to specify two functions to start when mouse pointer move over the selected element. Syntax: $(selector). hover(Function_in, Function_out);


3 Answers

The hover event in jQuery needs 2 callback functions: one when the pointer moves over the item, and one when it leaves:

$(item).hover(function() { ... }, function() { ... }); 

In your case:

$("a.button").hover(    function() {       $(this).animate({"marginTop": "0px"}, "fast");    },    function() {       $(this).animate({"marginTop": "16px"}, "fast");    } ); 
like image 153
Philippe Leybaert Avatar answered Sep 26 '22 03:09

Philippe Leybaert


In newer versions of jQuery (>=1.7) you can also take this approach:

$("a.button").on('mouseenter',function(){   $(this).animate({"marginTop": "0px"}, "fast"); }); $("a.button").on('mouseleave',function(){   $(this).animate({"marginTop": "16px"}, "fast"); }); 

In my opinion this is a cleaner approach, and it also takes advantage of the the new .on() function (documentation here)

like image 40
Heath Avatar answered Sep 26 '22 03:09

Heath


Simpler solution:

$("a.button").hover(function() {
  $("a.button").css("cursor","pointer");
});
like image 37
Kuharski Avatar answered Sep 27 '22 03:09

Kuharski