jquery動(dòng)畫實(shí)現(xiàn)是使用animate() 方法,所以這篇jquery學(xué)習(xí)教程就主要給大家講解一下animate() 方法。
animate() 方法
語法:
$(selector).animate({params},speed,callback);
必需的 params 參數(shù)定義形成動(dòng)畫的 CSS 屬性。
可選的 speed 參數(shù)規(guī)定效果的時(shí)長。它可以取以下值:"slow"、"fast" 或毫秒。
可選的 callback 參數(shù)是動(dòng)畫完成后所執(zhí)行的函數(shù)名稱。
注意:默認(rèn)情況下,所有 HTML 元素的位置都是靜態(tài)的,并且無法移動(dòng)。如需對位置進(jìn)行操作,記得首先把元素的 CSS position 屬性設(shè)置為 relative、fixed 或 absolute。animate可以使用預(yù)定義的值,在執(zhí)行的過程中如果有多個(gè)animate方法,則依次執(zhí)行(見例2)。
例1:
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({left:'250px'});
});
});
</script>
</head>
<body>
<button>開始動(dòng)畫</button>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
jquery還可以操作多個(gè)屬性:
例如:
$("button").click(function(){
$("div").animate({
left:'250px',
opacity:'0.5',
height:'150px',
width:'150px'
});
});
注意:
(1)幾乎可以用animate方法來操作所有的CSS屬性。但是所有css屬性需要用駝峰寫法,比如padding-left需要寫成paddingLeft
(2)生成顏色動(dòng)畫需要去jquery.com里面下載 Color Animations插件。這個(gè)插件不包括在jquery庫里面。
例2:
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var div=$("div");
div.animate({height:'300px',opacity:'0.4'},"slow");
div.animate({width:'300px',opacity:'0.8'},"slow");
div.animate({height:'100px',opacity:'0.4'},"slow");
div.animate({width:'100px',opacity:'0.8'},"slow");
});
});
</script>
</head>
<body>
<button>開始動(dòng)畫</button>
<p>默認(rèn)情況下,所有 HTML 元素的位置都是靜態(tài)的,并且無法移動(dòng)。如需對位置進(jìn)行操作,記得首先把元素的 CSS position 屬性設(shè)置為 relative、fixed 或 absolute。</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
文章來自:博客園/sophiehui