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