2013-10-09 68 views
1
$(".menuicon").mouseenter(function() { 
    $(this).animate({ 
     width: '100px' 
    }, 300); 
}); 

$(".menuicon").mouseover(function() { 
    $(this).animate({ 
     width: '36px' 
    }, 300); 
}); 

鼠標懸停時圖標的寬度更改爲100px,但很快會回到36px,鼠標仍然懸停在其上。鼠標懸停時動畫寬度不起作用

回答

3

您需要使用mouseleave事件,不mouseover

$(".menuicon").mouseleave(function() { 
    $(this).stop(true).animate({ 
     width: '36px' 
    }, 300); 
}); 

或者更好的是,結合整個事情用hover

$(".menuicon").hover(
    function() { $(this).stop(true).animate({ width: '100px' }, 300); }, 
    function() { $(this).stop(true).animate({ width: '36px' }, 300); } 
); 
+0

這工作。但問題是,當我快速鼠標懸停多次時,動畫不斷堆疊,並持續彈跳。任何方式來避免這種情況? – user1537779

+0

你可以用'停止(真)'清除動畫隊列。看到我的更新 –

+0

是的。非常感謝。 – user1537779

1

使用mouseleave()代替mouseover()一樣,

$(".menuicon").mouseleave(function() { 
    $(this).animate({ 
     width: '36px' 
    }, 300); 
}); 

或者嘗試hover()一樣,

$(".menuicon").hover(function() { 
    $(this).animate({ 
     width: '100px' 
    }, 300); 
},function() { 
    $(this).animate({ 
     width: '36px' 
    }, 300); 
}); 
0
$(".menuicon").hover(function() { 
    $(this).animate({ 
     width: '100px' 
    }, 300); 
},function() { 
    $(this).animate({ 
     width: '36px' 
    }, 300); 
});