概述 .fadeOut( [duration ] [, complete ] )
返回值:jQuery
描述:通过淡出的方式隐藏匹配元素。
400
)
400
)
swing
)
true
)
.dequeue("queuename")
来启动它。
400
)
swing
)
.fadeOut()
方法通过匹配元素的透明度执行动画效果。一旦透明度达到0,display
样式属性将被设置为none
,以确保该元素不再影响页面布局。
持续时间是以毫秒为单位的,数值越大,动画越慢,不是越快。字符串 'fast'
和 'slow'
分别代表200和600毫秒的延时。如果提供任何其他字符串,或者这个duration
参数被省略,那么默认使用400
毫秒的延时。
我们可以给任何元素做动画,比如一个简单的图片:
- <div id="clickme">
- Click here
- </div>
- <img id="book" src="book.png" alt="" width="100" height="123" />
该元素开始是显示的,我们可以将其缓缓的隐藏::
- $('#clickme').click(function() {
- $('#book').fadeOut('slow', function() {
- // Animation complete.
- });
- });
注意: 为了避免不必要的DOM操作,
.fadeOut()
不会隐藏已经被认为是隐藏的元素。有关jQuery的认为隐藏的元素,请参阅::hidden 选择器
。
从jQuery 1.4.3开始,增加了一个字符串命名的可选的参数,用于确定使用的缓动函数。一个缓动函数指定用于动画进行中在不同点位的速度。 在jQuery库中easing默认的是调用 swing
;如果想要在一个恒定的速度进行动画,那么调用 linear
. 更多的缓动函数要使用的插件,最显着的是jQuery UI suite。
如果提供回调函数,这个 callback
函数将在动画完成后被执行一次。这个能用来将不同的动画串联起来组成一个事件序列。这个回调函数不设置任何参数,但是this
指向执行动画的DOM元素。如果多个元素一起做动画效果,值得注意的是这个回调函数在每个匹配元素上执行一次。这个动画不是作为一个整体。
从jQuery 1.6开始,.promise()
方法可以用来配合deferred.done()
方法作为一个整体,当所有匹配的元素已经完成各自的动画后,再执行一个回调的动画。
.fadeOut()
,都能通过设置jQuery.fx.off = true
全局的关闭,效果等同于持续时间设置为0。更多信息查看 jQuery.fx.off
示例
让所有段落渐渐消失,用时 600 毫秒。
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- p { font-size:150%; cursor:pointer; }
- </style>
- <script src="http://code.jquery.com/jquery-latest.js"></script>
- </head>
- <body>
- <p>
- If you click on this paragraph
- you'll see it just fade away.
- </p>
- <script>
- $("p").click(function () {
- $("p").fadeOut("slow");
- });
- </script>
-
- </body>
- </html>
将你点击的 span 淡出隐藏。
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- span { cursor:pointer; }
- span.hilite { background:yellow; }
- div { display:inline; color:red; }
- </style>
- <script src="http://code.jquery.com/jquery-latest.js"></script>
- </head>
- <body>
- <h3>Find the modifiers - <div></div></h3>
- <p>
- If you <span>really</span> want to go outside
- <span>in the cold</span> then make sure to wear
- your <span>warm</span> jacket given to you by
- your <span>favorite</span> teacher.
- </p>
- <script>
-
- $("span").click(function () {
- $(this).fadeOut(1000, function () {
- $("div").text("'" + $(this).text() + "' has faded!");
- $(this).remove();
- });
- });
- $("span").hover(function () {
- $(this).addClass("hilite");
- }, function () {
- $(this).removeClass("hilite");
- });
-
- </script>
-
- </body>
- </html>
将两个 div 淡出隐藏。其中一个使用 "linear" 缓冲效果,另一个使用默认的 "swing," 缓冲效果。
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- .box,
- button { float:left; margin:5px 10px 5px 0; }
- .box { height:80px; width:80px; background:#090; }
- #log { clear:left; }
-
- </style>
- <script src="http://code.jquery.com/jquery-latest.js"></script>
- </head>
- <body>
-
- <button id="btn1">fade out</button>
- <button id="btn2">show</button>
-
- <div id="log"></div>
-
- <div id="box1" class="box">linear</div>
- <div id="box2" class="box">swing</div>
-
- <script>
- $("#btn1").click(function() {
- function complete() {
- $("<div/>").text(this.id).appendTo("#log");
- }
-
- $("#box1").fadeOut(1600, "linear", complete);
- $("#box2").fadeOut(1600, complete);
- });
-
- $("#btn2").click(function() {
- $("div").show();
- $("#log").empty();
- });
-
- </script>
-
- </body>
- </html>