我使用.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