我使用.mouseenter()和.mouseleave()做一個動畫到我的按鈕。問題是,當我多次移動我的光標在按鈕,它不斷重複.mouseenter()我希望它完成動畫,一旦光標保持懸停在其上面,直到動畫時間完成,並且如果它在動畫完成之前離開按鈕,動畫應該停止。對於.mouseleave()如果在動畫完成之前將光標懸停在其上,則應停止動畫。 問題與.mouseleave()和.mouseenter()
$(document).ready(function(){
$('#button').mouseenter(function(){
$(this).animate({backgroundColor:'#ffce00',width:'+=1em'},100);
});
$('#button').mouseleave(function(){
$(this).animate({backgroundColor:'#1e7989',width:'-=1em'},100);
});
});
#button{
width:100px;
height:50px;
background-color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/jquery.color-animation/1/mainfile"></script>
<div id="button"></div>
你提到它不斷重複動畫。你想要它做什麼? – jasonwarford
是否有一個原因,你這樣做與jQuery的?你可以用CSS實現同樣的效果。 – cocoa
這就是這些事件應該如何工作。如果您想停止動畫,請參閱http://stackoverflow.com/questions/18211280/how-to-stop-an-animation-queue-in-jquery – Teemu