jQuery 停止動畫
jQuery stop() 方法
#jQuery stop() 方法用於停止動畫或效果,在它們完成之前。
stop() 方法適用於所有 jQuery 效果函數(shù),包括滑動、淡入淡出和自訂動畫。
語法:
$(selector).stop(stopAll,goToEnd);
#可選的stopAll 參數(shù)規(guī)定是否應該清除動畫佇列。預設是 false,即僅停止活動的動畫,允許任何排入佇列的動畫向後執(zhí)行。
可選的 goToEnd 參數(shù)規(guī)定是否立即完成目前動畫。預設是 false。
因此,預設地,stop() 會清除在被選元素上指定的目前動畫。
下面的範例示範stop() 方法,不含參數(shù):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#div1").click(function(){ $("#div2").slideDown(5000); }); $("#stop").click(function(){ $("#div2").stop(); }); }); </script> <style type="text/css"> #div2,#div1 { padding:5px; text-align:center; background-color:#e5eecc; border:solid 1px #c3c3c3; } #div2 { padding:50px; display:none; } </style> </head> <body> <button id="stop">停止滑動</button> <div id="div1">點我向下滑動面板</div> <div id="div2">我們必須接受失望,因為它是有限的,但千萬不可失去希望,因為它是無窮的</div> </body> </html>
執(zhí)行程式嘗試
jQuery stop() 動畫(帶參數(shù))
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"> </script> <script> $(document).ready(function(){ $("#start").click(function(){ $("div").animate({left:'100px'},5000); $("div").animate({fontSize:'3em'},5000); }); $("#stop").click(function(){ $("div").stop(); }); $("#stop2").click(function(){ $("div").stop(true); }); $("#stop3").click(function(){ $("div").stop(true,true); }); }); </script> </head> <body> <button id="start">開始</button> <button id="stop">停止</button> <button id="stop2">停止所有</button> <button id="stop3">停止動畫,但完成動作</button> <p>點擊 "開始" 按鈕開始動畫。</p> <p>點擊 "停止" 按鈕停止當前激活的動畫,但之后我們能再動畫隊列中再次激活。</p> <p>點擊 "停止所有" 按鈕停止當前動畫,并清除動畫隊列,所以元素的所有動畫都會停止。</p> <p>點擊 "停止動畫,但完成動作" 快速完成動作,并停止它。</p> <div style="background:#98bf21;height:100px;width:200px;position:absolute;">HELLO</div> </body> </html>
運行程式嘗試