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?
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.
hover() is deprecated #66.
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.
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);
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"); } );
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)
Simpler solution:
$("a.button").hover(function() {
$("a.button").css("cursor","pointer");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With