问题描述:

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',});
  }
);
相关阅读:
Top