概述 .fadeIn( [duration ] [, complete ] )
返回值:jQuery
描述:通过淡入的方式显示匹配元素。
400
)
400
)
swing
)
true
)
.dequeue("queuename")
来启动它。
400
)
swing
)
.fadeIn()
方法通过匹配元素的不透明度做动画效果。这是一个和.fadeTo()
类似的方法,但那个方法不会隐藏的元素并可以指定最后的透明度值。
duration(延时时间)参数是以毫秒为单位的,数值越大,动画越慢,不是越快。字符串 'fast'
和 'slow'
分别代表200和600毫秒的延时。如果提供任何其他字符串,或者这个duration
参数被省略,那么默认使用400
毫秒的延时
我们可以给任何元素做动画,比如一个简单的图片:
- <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').fadeIn('slow', function() {
- // Animation complete
- });
- });
从jQuery 1.4.3开始,增加了一个字符串命名的可选的参数,用于确定使用的缓动函数。一个缓动函数指定用于动画进行中在不同点位的速度。 在jQuery库中easing默认的是调用 swing
, 如果想要在一个恒定的速度进行动画,那么调用 linear
. 更多的缓动函数要使用的插件,最显着的是jQuery UI suite。
如果提供回调函数,这个 callback
函数将在动画完成后被执行一次。这个能用来将不同的动画串联起来组成一个事件序列。这个回调函数不设置任何参数,但是this
指向执行动画的DOM元素。如果多个元素一起做动画效果,值得注意的是这个回调函数在每个匹配元素上执行一次。这个动画不是作为一个整体。
从jQuery 1.6开始,.promise()
方法可以用来配合deferred.done()
方法作为一个整体,当所有匹配的元素已经完成各自的动画后,再执行一个回调的动画。
.fadeIn()
,都能通过设置jQuery.fx.off = true
全局的关闭,效果等同于持续时间设置为0。更多信息查看 jQuery.fx.off
.
示例
淡出所有段落,在600毫秒内完成这些动画。
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- span { color:red; cursor:pointer; }
- div { margin:3px; width:80px; display:none;
- height:80px; float:left; }
- div#one { background:#f00; }
- div#two { background:#0f0; }
- div#three { background:#00f; }
- </style>
- <script src="http://code.jquery.com/jquery-latest.js"></script>
- </head>
- <body>
- <span>Click here...</span>
-
- <div id="one"></div>
- <div id="two"></div>
- <div id="three"></div>
- <script>
- $(document.body).click(function () {
- $("div:hidden:first").fadeIn("slow");
- });
- </script>
-
- </body>
- </html>
Fades a red block in over the text. Once the animation is done, it quickly fades in more text on top.
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- p { position:relative; width:400px; height:90px; }
- div { position:absolute; width:400px; height:65px;
- font-size:36px; text-align:center;
- color:yellow; background:red;
- padding-top:25px;
- top:0; left:0; display:none; }
- span { display:none; }
- </style>
- <script src="http://code.jquery.com/jquery-latest.js"></script>
- </head>
- <body>
- <p>
- Let it be known that the party of the first part
- and the party of the second part are henceforth
- and hereto directed to assess the allegations
- for factual correctness... (<a href="#">click!</a>)
- <div><span>CENSORED!</span></div>
-
- </p>
- <script>
- $("a").click(function () {
- $("div").fadeIn(3000, function () {
- $("span").fadeIn(100);
- });
- return false;
- });
- </script>
-
- </body>
- </html>