標籤:
問題:queue()方法?
tip0:
jquery從1.9版本以上就不支援toggle()方法。
// $("#panel h5.head").toggle(function(){// ...// },function(){// ... // });
以上不支援!以下支援
// $("#panel h5.head").click(function(){// $(this).next().toggle();// });
tip1:
用jquery做動畫效果要求在標準模式下,否則可能會引起動畫抖動。標準模式即要求檔案頭部包含如下的DTD定義:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
tip2:
jquery 中的任何動畫效果,都可以指定3種速度參數“slow”,“normal”,“fast”時間長度分別是”0.6秒“0.4秒”“”0.2秒“,當使用速度關 鍵字時要加引號,例如show("slow"),如果用數字則不需要引號,例如show(1000);即(1000毫秒)1秒鐘內顯示。
tip3:
callback回呼函數適用於jquery所有動畫效果方法,例如
$("#element").slideDown("normal",function(){ //在效果完成後做其他事情 })
tip4:
判斷是否處於動畫狀態
if(!$("#element").is(":animated")){// 如果當前沒有進行動畫,則添加新動畫 }
tip5:
(1)一組元素上的動畫效果
當在一個animate()方法中應用多個屬性時,動畫是同時發生的。
當以鏈式的寫法應用動畫方法時,動畫是按順序發生的(除非queue選項值為false)
(2)多組元素上的動畫效果
預設情況下,動畫都是同時發生的。
當以回調的形式應用動畫方式時(包括動畫的回呼函數和queue()方法的回呼函數),動畫是按照回調順序發生的
注意:在動畫方法中,其他非動畫方法會插隊,例如css()方法要使非動畫方法也按照順序執行,需要把這些方法寫在動畫方法的回呼函數中或者queue()方法中。
這個判斷方法在animate()動畫中經常用到,需要特別注意。避免動畫積累而導致的動畫與使用者的行為不一致。
1、show()和hide()
①注意hide()方法在將“內容”的display屬性值設定為"none"之前,會記住原先的display屬性值。當調用show()方法時,就會根據hide()方法記住的display屬性值來顯示元素。
③同時改變”內容“的高度、寬度和不透明度。
$("#panel h5.head").toggle(function(){ $(this).next().hide(600); },function(){ $(this).next().show(600); })
2、fadeIn()和fadeOut()
只改變元素的不透明度。
3、slideUp()和slideDown()
只改變元素的高度。
4、animate()自訂動畫
#panel{ position: relative; width:100px; height: 100px; border: 1px solid #0050d0; background:#96e555; cursor: pointer;}
注意:定義relative
①簡單的動畫
$("#panel").click(function(){ $(this).animate({left:"500px"},300);})
②累加、累減
$("#panel").click(function(){ $(this).animate({left:"+=500px"},300); })
③多重動畫
$("#panel").click(function(){ $(this).animate({left:"500px",height:"200px"},300); })
④按順序執行(推薦鏈式寫法)
$("#panel").click(function(){// $(this).animate({left:"500px"},300);// $(this).animate({height:"200px"},3000); $(this).animate({left:"500px"},300) .animate({height:"200px"},3000); })
⑤綜合動畫
$("#panel").css("opacity","0.5"); //設定不透明度 $("#panel").click(function(){ $(this).animate({left:"400px",height:"200px",opacity:"1"},3000) //① .animate({top:"200px",width:"200px"},3000) //② .fadeOut("slow"); //③// .css("border","5px solid blue"); //④// ③是在②之後執行(只有是動畫才加入到隊列),但若改為④,css()方法並不會加入到動畫隊列中,而是立即執行。// 想要css()排入佇列,只要使用回呼函數。// .animate({top:"200px",width:"200px"},3000,function(){// $(this).css("border","5px solid blue");// }) })
5、stop()停止動畫
stop([clearQueue],[gotoEnd])
注意:兩個參數Boolean值(ture或false)。clearQueue代表是否要清空未執行完的動畫隊列,gotoEnd代表是否將正在執行的動畫跳轉到末狀態。
如果是直接使用stop()方法,則會立即停止當前進行中的動畫,如果接下來還有動畫等待繼續進行,則以目前狀態開始接下來的動畫。
$("#panel").hover(function(){ $(this).stop() .animate({height:"150"},2000) //① 如果在此時觸發了游標移除事件。stop()是立即停止①執行②③④;stop(true)是立即停止①清空②執行③④ //stop(false,true)是立即跳到①的結束狀態,並執行②③④;stop(true,true)是立即跳到①的結束狀態清空②執行③④ .animate({width:"300"},3000); //② },function(){ $(this).stop() .animate({height:"22"},2000) //③ .animate({width:"60"},3000); //④ })
注意:jquery只能設定正在執行的動畫的最終狀態,而沒有提供直接到達未執行動畫隊列最終狀態的方法。
6、delay()延遲動畫
$("#panel").css("opacity","0.5"); $("#panel").click(function(){ $(this).animate({left:"400px",height:"200px",opacity:"1"},3000)//① .delay(1000) //執行完①後等待1s再執行② .animate({top:"200px",width:"200px"},3000) //② .delay(2000) //執行完②後等待2s再執行③ .fadeOut("slow"); //③ })
7、其它動畫方法
toggle(speed,[callback]);
slideToggle(speed,[easing],[callback]); //改變高度
fadeToggle(speed,[easing],[callback]); //改變透明度
fadeTo(speed,opacity,[callback]);
$("#panel h5.head").click(function(){ $(this).next().toggle(); });// 相當於// $("#panel h5.head").toggle(function(){// $(this).next().hide();// },function(){// $(this).next().show();// }); $("#panel h5.head").click(function(){ $(this).next().slideToggle(); });// 相當於// $("#panel h5.head").toggle(function(){// $(this).next().slideUp();// },function(){// $(this).next().slideDown();// }); $("#panel h5.head").click(function(){ $(this).next().fadeToggle(); });// 相當於// $("#panel h5.head").toggle(function(){// $(this).next().fadeOut();// },function(){// $(this).next().fadeIn();// }); $("#panel h5.head").click(function(){ $(this).next().fadeTo(600,0.2); })
jquery中的動畫