queue()
queue()方法可以接受一個可選參數(shù),可以得到動畫隊列的長度。
queue()方法可以接受一個回調(diào)函數(shù)作為參數(shù),表示將要添加到隊列中的新函數(shù)。但回調(diào)函數(shù)中,可以進行樣式變換等,但不可以增加動畫效果。
隊列執(zhí)行完queue()的函數(shù)后,隊列后面的動畫效果被停止,這時就需要用到dequeue()方法
dequeue()
dequeue()方法用來執(zhí)行匹配元素隊列的下一個函數(shù)
clearQueue()
與deQueue()方法相反,clearQueue()方法用來從列隊中移除所有未執(zhí)行的項,但并不影響當(dāng)前動畫效果
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>demo</title>
<style>
div {
margin-top:50px;
width: 50px;
position: absolute;
height: 50px;
left: 10px;
top: 30px;
background-color: yellow;
}
div.red {
background-color: red;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p id="num">0</p>
<button>Start</button>
<button class="clear">Clear</buttton>
<div></div>
<script>
$( "button" ).click(function() {
showIt()
$( "div" )
.animate({ left:"+=200px" }, 2000 )
.animate({ top:"0px" }, 600 )
.queue(function() {
$( this ).toggleClass( "red" ).dequeue();
})
.animate({ left:"10px", top:"30px" }, 700 );
});
function showIt(){
var num = $('div').queue().length
$('#num').text(num);
setTimeout(showIt,10)
}
$('.clear').click(function(){
$( "div" ).clearQueue()
})
</script>
</body>
</html>