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




例子:
舉例: 點(diǎn)擊鏈接隱藏所有段落。
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<p>Hello</p>
<a href="#">Click to hide me too</a>
<p>Here is another paragraph</p>
<script>
$("p").hide();
$("a").click(function () {
$(this).hide();
return true;
});
</script>
</body>
</html>
Demo:
舉例: 緩慢的隱藏所有顯示的段落,600毫秒內(nèi)完成的動(dòng)畫(huà)。
<!DOCTYPE html>
<html>
<head>
<style>
p { background:#dad; font-weight:bold; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<button>Hide 'em</button>
<p>Hiya</p>
<p>Such interesting text, eh?</p>
<script>
$("button").click(function () {
$("p").hide("slow");
});
</script>
</body>
</html>
Demo:
舉例: Animates all spans (words in this case) to hide fastly, completing each animation within 200 milliseconds. Once each animation is done, it starts the next one.
<!DOCTYPE html>
<html>
<head>
<style>
span { background:#def3ca; padding:3px; float:left; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<button id="hidr">Hide</button>
<button id="showr">Show</button>
<div>
<span>Once</span> <span>upon</span> <span>a</span>
<span>time</span> <span>there</span> <span>were</span>
<span>three</span> <span>programmers...</span>
</div>
<script>
$("#hidr").click(function () {
$("span:last-child").hide("fast", function () {
// use callee so don't have to name the function
$(this).prev().hide("fast", arguments.callee);
});
});
$("#showr").click(function () {
$("span").show(2000);
});
</script>
</body>
</html>
Demo:
Example: Hides the divs when clicked over 2 seconds, then removes the div element when its hidden. Try clicking on more than one box at a time.
<!DOCTYPE html>
<html>
<head>
<style>
div { background:#ece023; width:30px;
height:40px; margin:2px; float:left; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<div></div>
<script>
for (var i = 0; i < 5; i++) {
$("<div>").appendTo(document.body);
}
$("div").click(function () {
$(this).hide(2000, function () {
$(this).remove();
});
});
</script>
</body>
</html>