fadeIn()
通过淡入的方式显示匹配元素。
.fadeIn([duration ][, complete ])
- duration(默认:
400
)类型: Number or String一个字符串或者数字决定动画将运行多久。 - complete类型:Function()。在动画完成时执行的函数。
.fadeIn(options)
options类型: PlainObject。一组包含动画选项的值的集合。支持的选项:
- duration(default:
400
)类型: Number or String一个字符串或者数字决定动画将运行多久。(注:默认值:"normal",三种预定速度的字符串("slow","normal",或"fast")或表示动画时长的毫秒数值(如:1000)) - easing(default:
swing
)类型: String。一个字符串,表示过渡使用哪种缓动函数。(注:jQuery自身提供"linear"和"swing",其他效果可以使用jQuery Easing Plugin插件) - queue(default:
true
)类型: Boolean or String 一个布尔值,指示是否将动画放置在效果队列中。如果为false时,将立即开始动画。从jQuery1.7开始,队列选项也可以接受一个字符串,在这种情况下,在动画被添加到由该字符串表示的队列中。当一个自定义的队列名称被使用,动画不会自动启动;你必须调用.dequeue("queuename")
来启动它。 - specialEasing类型: PlainObject。一组一个或多个通过相应的参数和相对简单函数定义的CSS属性(1.4 新增)
- step类型: Function(Number now, Tween tween)每个动画元素的每个动画属性将调用的函数。这个函数为修改Tween 对象提供了一个机会来改变设置中得属性值。
- progress类型: Function(Promise animation, Number progress, Number remainingMs)每一步动画完成后调用的一个函数,无论动画属性有多少,每个动画元素都执行单独的函数。(version added: 1.8)
- complete类型: Function()在动画完成时执行的函数。
- done类型: Function(Promise animation, Boolean jumpedToEnd)在动画完成时执行的函数。(他的Promise对象状态已完成).(version added: 1.8)
- fail类型: Function(Promise animation, Boolean jumpedToEnd)动画失败完成时执行的函数。(他的Promise对象状态未完成)。(version added: 1.8)
- always类型: Function(Promise animation, Boolean jumpedToEnd)在动画完成或未完成情况下停止时执行的函数。(他的Promise对象状态已完成或未完成)。(version added: 1.8)
.fadeIn([duration ][, easing ][, complete ])
- duration(默认:
400
)类型: Number or String一个字符串或者数字决定动画将运行多久。 - easing(默认:
swing
)类型: String。一个字符串,表示过渡使用哪种缓动函数。(译者注:jQuery自身提供"linear"和"swing",其他可以使用相关的插件) - complete类型:Function()。在动画完成时执行的函数。
.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 }); });
Easing(缓冲函数)
从jQuery 1.4.3开始,增加了一个字符串命名的可选的参数,用于确定使用的缓动函数。一个缓动函数指定用于动画进行中在不同点位的速度。在jQuery库中easing默认的是调用swing
,如果想要在一个恒定的速度进行动画,那么调用linear
.更多的缓动函数要使用的插件,最显着的是jQuery UI suite(译者注:或jQuery Easing Plugin插件)。
Callback Function(回调函数)
如果提供回调函数,这个callback
函数将在动画完成后被执行一次。这个能用来将不同的动画串联起来组成一个事件序列。这个回调函数不设置任何参数,但是this
指向执行动画的DOM元素。如果多个元素一起做动画效果,值得注意的是这个回调函数在每个匹配元素上执行一次。这个动画不是作为一个整体。
从jQuery 1.6开始,.promise()
方法可以用来配合deferred.done()
方法作为一个整体,当所有匹配的元素已经完成各自的动画后,再执行一个回调的动画。(查看.promise()例子)。
Additional Notes:(其他注意事项:)
- 所有jQuery效果,包括
.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="https://www.lanmper.cn/static/js/jquery-3.5.0.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>
在文本上淡出一个红色块。一旦动画完成,它很快在顶部的更多文本中淡出。
<!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="https://www.lanmper.cn/static/js/jquery-3.5.0.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="https://www.lanmper.cn">click!</a>) <div><span>CENSORED!</span></div> </p> <script> $("a").click(function () { $("div").fadeIn(3000,function () { $("span").fadeIn(100); });return false; }); </script> </body> </html>
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...(click!)