将 mousenter / mouseleave 组合成一个函数



考虑这两个工作函数。有没有办法将这两者串成一个jQuery函数?

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

请参阅 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);

$("#help").on('mouseenter mouseleave', function() {
});

替代使用:

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

最新更新