2009-07-12 62 views
27

我想要一个简单的向下/向上滑动鼠标滑过链接。我可以将鼠标移到工作位置,但我无法弄清楚如何让鼠标移动到该位置。jquery hover mouse out

这里是我有悬停效果:

<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> 

我如何得到这个移动的利润率高达16像素时鼠标呢?

+0

本例中的代码是无效的优势!也许你错过了`.hover(func,func)`的第二个函数? – Boldewyn 2009-07-12 16:29:20

回答

76

jQuery的悬停事件需要2个回调函数:一是当它离开在该项目上的指针移动,以及一个:

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

你的情况:

$("a.button").hover(
    function() { 
     $(this).animate({"marginTop": "0px"}, "fast"); 
    }, 
    function() { 
     $(this).animate({"marginTop": "16px"}, "fast"); 
    } 
); 
+0

这太棒了! – Mike 2012-08-17 15:00:42

+0

史诗般的答案之一! – 2016-06-22 16:12:52

1

简单的解决方案:

$("a.button").hover(function() { 
    $("a.button").css("cursor","pointer"); 
}); 
18

在较新版本的jQuery(> = 1.7)中,您也可以采用这种方法:

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

在我看来,这是一个更简洁的方法,而且还采取了新的。对()函数(documentation here