1

これらの2つの機能について考えてみましょう。これら2つを1つのjQuery関数にまとめる方法はありますか?

$("#help").mouseenter(function(){
    $(this).animate({bottom: '+=100',});
});

$("#help").mouseleave(function(){
    $(this).animate({bottom: '-=100',});
});
4

2 に答える 2

3

http://api.jquery.com/hover/を参照してください

$("#help").hover(function() {
    $(this).animate({
        bottom: '+=100',
    });
}, function() {
    $(this).animate({
        bottom: '-=100',
    });
});​

.hover()メソッドは、mouseenterイベントとmouseleaveイベントの両方のハンドラーをバインドします。これを使用して、マウスが要素内にあるときに要素に動作を適用することができます。呼び出し$(selector).hover(handlerIn, > handlerOut)は次の略記です。$(selector).mouseenter(handlerIn).mouseleave(handlerOut);

于 2012-05-16T16:05:59.793 に答える
0
$("#help").on('mouseenter mouseleave', function() {

});

代わりに使用:

$('#help').hover(
  function() {
     // code for mouseenter 
     $(this).animate({bottom: '+=100',});
  },

  function() {
    // code for mouseleave
    $(this).animate({bottom: '-=100',});
  }
);
于 2012-05-16T16:06:17.907 に答える