.show()
.show( ) 返回: jQuery
說明: 顯示匹配的元素。
version added: 1.0.show()
-
version added: 1.0.show( duration, [ callback ] )
duration一個(gè)字符串或者數(shù)字決定動畫將運(yùn)行多久。
callback在動畫完成時(shí)執(zhí)行的函數(shù)。
-
version added: 1.4.3.show( [ duration ], [ easing ], [ callback ] )
duration一個(gè)字符串或者數(shù)字決定動畫將運(yùn)行多久。
easing一個(gè)用來表示使用哪個(gè)緩沖函數(shù)來過渡的字符串。
callback在動畫完成時(shí)執(zhí)行的函數(shù)。
如果沒有參數(shù),.show()方法是最簡單的方法來顯示一個(gè)元素:
$('.target').show();
匹配的元素將被立即顯示,沒有動畫。這大致相當(dāng)于調(diào)用.css('display', 'block'),但display屬性值保存在jQuery的數(shù)據(jù)緩存中,所以display可以方便以后可以恢復(fù)到其初始值。如果一個(gè)元素的display屬性值為inline,然后是隱藏和顯示,這個(gè)元素將再次顯示inline。
當(dāng)提供一個(gè)持續(xù)時(shí)間參數(shù),.show()成為一個(gè)動畫方法。.show()方法將為匹配元素的寬度,高度,以及不透明度,同時(shí)進(jìn)行動畫。
持續(xù)時(shí)間是以毫秒為單位的,數(shù)值越大,動畫越慢,不是越快。字符串 'fast' 和 'slow' 分別代表200和600毫秒的延時(shí)。
如果提供回調(diào)函數(shù)參數(shù),回調(diào)函數(shù)會在動畫完成的時(shí)候調(diào)用。這個(gè)對于將不同的動畫串聯(lián)在一起按順序排列是非常有用的。這個(gè)回調(diào)函數(shù)不設(shè)置任何參數(shù),但是this是存在動畫的DOM元素,如果多個(gè)元素一起做動畫效果,值得注意的是每執(zhí)行一次回調(diào)匹配的元素,而不是作為一個(gè)整體的動畫一次。
我們可以給任何元素做動畫,比如一個(gè)簡單的圖片:
<div id="clickme">
Click here
</div>
<img id="book" src="book.png" alt="" width="100" height="123" />
With the element initially hidden, we can show it slowly:
$('#clickme').click(function() {
$('#book').show('slow', function() {
// Animation complete.
});
});




Examples:
Example: 緩慢地顯示所有隱藏的段落,600毫秒內(nèi)完成的動畫。
<!DOCTYPE html>
<html>
<head>
<style>
p { background:yellow; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<button>Show it</button>
<p style="display: none">Hello 2</p>
<script>
$("button").click(function () {
$("p").show("slow");
});
</script>
</body>
</html>
Demo:
Example: Animates all hidden divs to show fastly in order, completing each animation within 200 milliseconds. Once each animation is done, it starts the next one.
<!DOCTYPE html>
<html>
<head>
<style>
div { background:#def3ca; margin:3px; width:80px;
display:none; float:left; text-align:center; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<button id="showr">Show</button>
<button id="hidr">Hide</button>
<div>Hello 3,</div>
<div>how</div>
<div>are</div>
<div>you?</div>
<script>
$("#showr").click(function () {
$("div:eq(0)").show("fast", function () {
/* use callee so don't have to name the function */
$(this).next("div").show("fast", arguments.callee);
});
});
$("#hidr").click(function () {
$("div").hide(2000);
});
</script>
</body>
</html>
Demo:
Example: Shows all span and input elements with an animation. Once the animation is done, it changes the text.
<!DOCTYPE html>
<html>
<head>
<style>
span { display:none; }
div { display:none; }
p { font-weight:bold; background-color:#fcd; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<button>Do it!</button>
<span>Are you sure? (type 'yes' if you are) </span>
<div>
<form>
<input type="text" value="as;ldkfjalsdf"/>
</form>
</div>
<p style="display:none;">I'm hidden...</p>
<script>
function doIt() {
$("span,div").show("slow");
}
/* can pass in function name */
$("button").click(doIt);
$("form").submit(function () {
if ($("input").val() == "yes") {
$("p").show(4000, function () {
$(this).text("Ok, DONE! (now showing)");
});
}
$("span,div").hide("fast");
/* to stop the submit */
return false;
});
</script>
</body>
</html>