用於創建自定義動畫的函數。
返回值:jQuery animate(params, [duration], [easing], [callback])
如果使用的是“hide”、“show”或“toggle”這樣的字符串值,則會為該屬性調用默認的動畫形式。paramsOptions一組包
含作為動畫屬性和終值的樣式屬性和及其值的集合
params 對象{},注意:所有指定的屬性必須用駱駝形式,比如用marginLeft代替margin-left,如果使用的是“hide”、
“show”或“toggle”這樣的字符串值,則會為該屬性調用默認的動畫形式。
duration (可選)三種預定速度之一的字符串("slow", "normal", or "fast")或表示動畫時長的毫秒數值(如:1000)
easing (可選)String要使用的擦除效果的名稱(需要插件支持).默認jQuery提供"linear" 和 "swing"
callback (可選)Function在動畫完成時執行的函數
0.停止動畫
if($('.swaplist,.mainlist').is(':animated')){ $('.swaplist,.mainlist').stop(true,true); }
animate實例:
1.點擊按鈕后div元素的幾個不同屬性一同變化
$("#go").click(function () { $("#block").animate({ width: "90%", height: "100%", fontSize: "10em", borderWidth: 10 }, 1000); });
2.讓指定元素左右移動
$("#right").click(function () { $(".block").animate({ left: '+50px' }, "slow"); }); $("#left").click(function () { $(".block").animate({ left: '-50px' }, "slow"); });
3.在600毫秒內切換段落的高度和透明度
$("p").animate({ height: 'toggle', opacity: 'toggle' }, "slow");
4.用500毫秒將段落移到left為50的地方並且完全清晰顯示出來(透明度為1)
$("p").animate({ left: 50, opacity: 'show' }, 500);
5.切換顯示隱藏
$(".box h3").toggle(function(){ $(this).next(".text").animate({height: 'toggle', opacity: 'toggle'}, "slow"); $(this).addClass("arrow"); return false; },function(){ $(this).next(".text").animate({height: 'toggle', opacity: 'toggle'}, "slow"); $(this).removeClass("arrow"); return false; }); });
//滾動焦點 $(window).scroll(function () { //當前窗口的滾動事件 var winTop = $(window).scrollTop(); //獲取當前窗口的大小 var objTop = $("#obj1").offset().top; //獲取當前對象的x坐標 });