2012-05-16 39 views
1

考虑这两个工作函数。有什么办法将这两个字符串串成一个jQuery函数吗?将mousenter/mouseleave组合成一个函数

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

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

回答

3

参见http://api.jquery.com/hover/

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

的.hover()方法结合处理程序两者的mouseenter和鼠标离开 事件。当鼠标位于元素内时,您可以使用它在 期间将行为简单应用到元素。调用$(selector).hover(handlerIn, > handlerOut)是简写:$(selector).mouseenter(handlerIn).mouseleave(handlerOut);

+0

美丽。谢谢@ j08691 –

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

}); 

交替使用:

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

    function() { 
    // code for mouseleave 
    $(this).animate({bottom: '-=100',}); 
    } 
); 
相关问题