.fadeTo()
.fadeTo( duration, opacity, [ callback ] ) 返回: jQuery
描述: 調(diào)整匹配元素的透明度。
-
version added: 1.0.fadeTo( duration, opacity, [ callback ] )
duration一個(gè)字符串或者數(shù)字決定動(dòng)畫將運(yùn)行多久。
opacity一個(gè)0至1之間表示目標(biāo)透明度的數(shù)字。
callback在動(dòng)畫完成時(shí)執(zhí)行的函數(shù)。
-
version added: 1.4.3.fadeTo( [ duration ], opacity, [ easing ], [ callback ] )
duration一個(gè)字符串或者數(shù)字決定動(dòng)畫將運(yùn)行多久。
opacity一個(gè)0至1之間表示目標(biāo)透明度的數(shù)字。
easing一個(gè)用來表示使用哪個(gè)緩沖函數(shù)來過渡的字符串
callback在動(dòng)畫完成時(shí)執(zhí)行的函數(shù)。
.fadeTo() 方法通過匹配元素的不透明度做動(dòng)畫效果。
延時(shí)時(shí)間是以毫秒為單位的,數(shù)值越大,動(dòng)畫越慢,不是越快。字符串 'fast' 和 'slow' 分別代表200和600毫秒的延時(shí)。如果提供任何其他字符串,或者這個(gè)duration參數(shù)被省略,那么默認(rèn)使用400毫秒的延時(shí)。和其他效果方法不同,.fadeTo()需要明確的指定duration參數(shù)。
如果提供回調(diào)函數(shù)參數(shù),回調(diào)函數(shù)會在動(dòng)畫完成的時(shí)候調(diào)用。這個(gè)對于將不同的動(dòng)畫串聯(lián)在一起按順序排列是非常有用的。這個(gè)回調(diào)函數(shù)不設(shè)置任何參數(shù),但是this是存在動(dòng)畫的DOM元素,如果多個(gè)元素一起做動(dòng)畫效果,值得注意的是這個(gè)回調(diào)函數(shù)在每個(gè)匹配元素上執(zhí)行一次,不是這個(gè)動(dòng)畫作為一個(gè)整體。
我們可以給任何元素做動(dòng)畫,比如一個(gè)簡單的圖片:
<div id="clickme">
Click here
</div>
<img id="book" src="book.png" alt="" width="100" height="123" />
With the element initially shown, we can dim it slowly:
$('#clickme').click(function() {
$('#book').fadeTo('slow', 0.5, function() {
// Animation complete.
});
});




將duration設(shè)置為0,這個(gè)方法只是改變opacity CSS屬性,所以.fadeTo(0, opacity)和.css('opacity', opacity)是一樣的效果。
注意:
-
所有的jQuery效果,包括
.fadeTo(),能使用jQuery.fx.off = true關(guān)閉全局性。更多信息請查看jQuery.fx.off。
例子:
舉例: 將第一個(gè)段落的不透明度設(shè)置成0.33(33%),在600毫秒內(nèi)完成這些動(dòng)畫。
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<p>
Click this paragraph to see it fade.
</p>
<p>
Compare to this one that won't fade.
</p>
<script>
$("p:first").click(function () {
$(this).fadeTo("slow", 0.33);
});
</script>
</body>
</html>
Demo:
Example: 點(diǎn)擊將div設(shè)置成隨機(jī)的不透明度,在200毫秒內(nèi)完成這些動(dòng)畫。
<!DOCTYPE html>
<html>
<head>
<style>
p { width:80px; margin:0; padding:5px; }
div { width:40px; height:40px; position:absolute; }
div#one { top:0; left:0; background:#f00; }
div#two { top:20px; left:20px; background:#0f0; }
div#three { top:40px; left:40px; background:#00f; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<p>And this is the library that John built...</p>
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<script>
$("div").click(function () {
$(this).fadeTo("fast", Math.random());
});
</script>
</body>
</html>
Demo:
Example: Find the right answer! The fade will take 250 milliseconds and change various styles when it completes.
<!DOCTYPE html>
<html>
<head>
<style>
div, p { width:80px; height:40px; top:0; margin:0;
position:absolute; padding-top:8px; }
p { background:#fcc; text-align:center; }
div { background:blue; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<p>Wrong</p>
<div></div>
<p>Wrong</p>
<div></div>
<p>Right!</p>
<div></div>
<script>
var getPos = function (n) {
return (Math.floor(n) * 90) + "px";
};
$("p").each(function (n) {
var r = Math.floor(Math.random() * 3);
var tmp = $(this).text();
$(this).text($("p:eq(" + r + ")").text());
$("p:eq(" + r + ")").text(tmp);
$(this).css("left", getPos(n));
});
$("div").each(function (n) {
$(this).css("left", getPos(n));
})
.css("cursor", "pointer")
.click(function () {
$(this).fadeTo(250, 0.25, function () {
$(this).css("cursor", "")
.prev().css({"font-weight": "bolder",
"font-style": "italic"});
});
});
</script>
</body>
</html>