jQuery動畫具有按序列執行的特點,即第一個動畫執行完會繼續執行后續動畫,好多時候,我們都需要停止當前動畫,或立即執行完當前動畫,這時,就需要用到jQuery提供到的stop()方法。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
width: 100px;
height: 100px;
background-color: skyblue;
}
</style>
<script src="../jquery-1.11.3.min.js"></script>
<script>
$(function () {
$("button").eq(0).click(function () {
// stop(false) 停止當前動畫,后續隊列中的動畫繼續執行 (默認值)
// stop(true) 停止當前動畫,并清空后續隊列中的所有動畫
// 第二個參數默認值也為false
// stop(false,true) 當前動畫立即執行完畢,后續隊列中的動畫繼續執行
// stop(true,true) 當前動畫立即執行完畢,并清空后續隊列中的所有動畫
$("div").animate({"height": "300"}, 2000);
$("div").animate({"width": "300"}, 2000);
$("div").animate({"height": "100"}, 2000);
$("div").animate({"width": "100"}, 2000);
})
$("button").eq(1).click(function () {
$("div").stop(true,true);
})
})
</script>
</head>
<body>
<button>開始</button>
<button>停止</button>
<div></div>
</body>
</html>
大家可以根據具體情況,選擇采用哪種參數來實現自己想要的效果