jquery将鼠标悬停在外面

Stu*_*son 26 jquery hover

我希望鼠标悬停在链接上的简单向下滑动动画.我可以让鼠标工作但我无法弄清楚如何让mouseout做它的事情.

这是我对悬停效果的看法:

<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>
Run Code Online (Sandbox Code Playgroud)

如何在鼠标移出时将此余量提高16px?

Phi*_*ert 79

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

$(item).hover(function() { ... }, function() { ... });
Run Code Online (Sandbox Code Playgroud)

在你的情况下:

$("a.button").hover(
   function() {
      $(this).animate({"marginTop": "0px"}, "fast");
   },
   function() {
      $(this).animate({"marginTop": "16px"}, "fast");
   }
);
Run Code Online (Sandbox Code Playgroud)


Hea*_*ath 20

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

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

在我看来,这是一种更简洁的方法,它还利用了新的.on()函数(此处的文档)