每当我将 Velocity 用于悬停效果时,我都会使用 data 属性来确保动画正在做正确的事情。@MeTe-30 的回答对我来说表现不佳。此解决方案可能对您更有用:DEMO
var container = $('.icon-container');
var icon = container.find('p');
var text = container.find('span');
var group = icon.add(text);
container.data({animating:false}); // add data for animation queue purposes
function showIconPicture() {
/*
if the path is in the middle of an animation, stop it immediately and reverse the animation. This prevents many unwanted animations if the user hovers in and out quickly
*/
if (container.data('animating') === true){
icon.velocity("stop", true).velocity('reverse',{ duration:0});
container.data({animating:false});
} else { // begin default animation
icon.velocity({right :60, opacity: 1},{
duration:300,
begin: function(){
container.data({animating:true});
},
complete: function() {
text.velocity({ opacity:1 }, {duration: 300, delay: 0});
container.data({animating:false});
}
});
} // end of conditional statement
} // end of function
function hideIconPicture() {
/*
if the path is in the middle of an animation, stop it immediately and reverse the animation. This prevents many unwanted animations if the user hovers in and out quickly
*/
if (container.data('animating') === true){
icon.velocity("stop", true).velocity('reverse',{ duration:0});
container.data({animating:false});
} else { // begin default animation
icon.velocity({ right: 0, opacity:0 }, {
duration:200,
begin: function(){
container.data({animating:true});
},
complete: function(){
group.velocity({ opacity:0 },{ duration:200 });
container.data({animating:false});
}
});
} // end of conditional statement
} // end of function
$(".cover-profile").hover(showIconPicture, hideIconPicture);