当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - Combining mousenter / mouseleave into one function

问题描述:

Consider these two working functions. Is there any way to string these two together into one jQuery function?

$("#help").mouseenter(function(){

$(this).animate({bottom: '+=100',});

});

$("#help").mouseleave(function(){

$(this).animate({bottom: '-=100',});

});

网友答案:

See http://api.jquery.com/hover/

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

The .hover() method binds handlers for both mouseenter and mouseleave events. You can use it to simply apply behavior to an element during the time the mouse is within the element. Calling $(selector).hover(handlerIn, > handlerOut) is shorthand for: $(selector).mouseenter(handlerIn).mouseleave(handlerOut);

网友答案:
$("#help").on('mouseenter mouseleave', function() {

});

alternatively used:

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

  function() {
    // code for mouseleave
    $(this).animate({bottom: '-=100',});
  }
);
分享给朋友:
您可能感兴趣的文章:
随机阅读: