.hide()
.hide( ) Returns: jQuery
Description: 隱藏匹配的元素。
version added: 1.0.hide()
-
version added: 1.0.hide( duration, [ callback ] )
duration一個字符串或者數(shù)字決定動畫將運(yùn)行多久。
callback在動畫完成時執(zhí)行的函數(shù)。
-
version added: 1.4.3.hide( [ duration ], [ easing ], [ callback ] )
duration一個字符串或者數(shù)字決定動畫將運(yùn)行多久。
easing一個字符串,指示該函數(shù)使用緩和的過渡。
callback在動畫完成時執(zhí)行的函數(shù)。
如果沒有參數(shù),.hide()
方法是最簡單的方法來隱藏一個元素:
$('.target').hide();
匹配的元素將被立即隱藏,沒有動畫。這大致相當(dāng)于調(diào)用.css('display', 'none')
,但display
屬性值保存在jQuery的數(shù)據(jù)緩存中,所以display
可以方便以后可以恢復(fù)到其初始值。如果一個元素的display
屬性值為inline
,然后是隱藏和顯示,這個元素將再次顯示inline
。
當(dāng)提供一個持續(xù)時間參數(shù),.hide()
成為一個動畫方法。.hide()
方法將為匹配元素的寬度,高度,以及不透明度,同時進(jìn)行動畫。一旦透明度達(dá)到0,display
樣式屬性將被設(shè)置為none
,這個元素將不再在頁面中影響布局。
持續(xù)時間是以毫秒為單位的,數(shù)值越大,動畫越慢,不是越快。字符串 'fast'
和 'slow'
分別代表200和600毫秒的延時。
如果提供回調(diào)函數(shù)參數(shù),回調(diào)函數(shù)會在動畫完成的時候調(diào)用。這個對于將不同的動畫串聯(lián)在一起按順序排列是非常有用的。這個回調(diào)函數(shù)不設(shè)置任何參數(shù),但是this
是存在動畫的DOM元素,如果多個元素一起做動畫效果,值得注意的是每執(zhí)行一次回調(diào)匹配的元素,而不是作為一個整體的動畫一次。
我們可以給任何元素做動畫,比如一個簡單的圖片:
<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.'); }); });
例子:
舉例: 點擊鏈接隱藏所有段落。
<!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)完成的動畫。
<!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>