加载中...

fadeIn()


概述    .fadeIn( [duration ] [, complete ] )

返回值:jQuery

描述:通过淡入的方式显示匹配元素。

  • V : 1.0.fadeIn( [duration ] [, complete ] )

    • duration (默认: 400)
      类型: Number or String
      一个字符串或者数字决定动画将运行多久。
    • complete
      类型: Function()
      在动画完成时执行的函数。
  • V : 1.0.fadeIn( options )

    • options
      类型: PlainObject
      一组包含动画选项的值的集合。 支持的选项:
      • duration (default: 400)
        Type: Number or String
        一个字符串或者数字决定动画将运行多久。(手册网注:默认值: "normal", 三种预定速度的字符串("slow", "normal", 或 "fast")或表示动画时长的毫秒数值(如:1000) )
      • easing (default: swing)
        Type: String
        一个字符串,表示过渡使用哪种缓动函数。
      • queue (default: true)
        Type: Boolean or String
        一个布尔值,指示是否将动画放置在效果队列中。如果为false时,将立即开始动画。 从jQuery1.7开始,队列选项也可以接受一个字符串,在这种情况下,在动画被添加到由该字符串表示的队列中。当一个自定义的队列名称被使用,动画不会自动启动;你必须调用.dequeue("queuename")来启动它。
      • specialEasing
        Type: PlainObject
        一组一个或多个通过相应的参数和相对简单函数定义的CSS属性 ( 1.4 新增)
      • step
        Type: Function( Number now, Tween tween )
        每个动画元素的每个动画属性将调用的函数。这个函数为修改Tween 对象提供了一个机会来改变设置中得属性值。
      • progress
        Type: Function( Promise animation, Number progress, Number remainingMs )
        每一步动画完成后调用的一个函数,无论动画属性有多少,每个动画元素都执行单独的函数。 (version added: 1.8)
      • complete
        Type: Function()
        在动画完成时执行的函数。
      • done
        Type: Function( Promise animation, Boolean jumpedToEnd )
        在动画完成时执行的函数。 (他的Promise对象状态已完成). (version added: 1.8)
      • fail
        Type: Function( Promise animation, Boolean jumpedToEnd )
        动画失败完成时执行的函数。(他的Promise对象状态未完成)。 (version added: 1.8)
      • always
        Type: Function( Promise animation, Boolean jumpedToEnd )
        在动画完成或未完成情况下停止时执行的函数。(他的Promise对象状态已完成或未完成)。 (version added: 1.8)
  • V : 1.4.3.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毫秒的延时

我们可以给任何元素做动画,比如一个简单的图片:

  1. <div id="clickme">
  2. Click here
  3. </div>
  4. <img id="book" src="book.png" alt="" width="100" height="123" />
  5. With the element initially hidden, we can show it slowly:
  6. $('#clickme').click(function() {
  7. $('#book').fadeIn('slow', function() {
  8. // Animation complete
  9. });
  10. });

Easing(缓冲函数)

从jQuery 1.4.3开始,增加了一个字符串命名的可选的参数,用于确定使用的缓动函数。一个缓动函数指定用于动画进行中在不同点位的速度。 在jQuery库中easing默认的是调用 swing, 如果想要在一个恒定的速度进行动画,那么调用 linear. 更多的缓动函数要使用的插件,最显着的是jQuery UI suite

Callback Function(回调函数)

如果提供回调函数,这个 callback 函数将在动画完成后被执行一次。这个能用来将不同的动画串联起来组成一个事件序列。这个回调函数不设置任何参数,但是this指向执行动画的DOM元素。如果多个元素一起做动画效果,值得注意的是这个回调函数在每个匹配元素上执行一次。这个动画不是作为一个整体。

从jQuery 1.6开始.promise()方法可以用来配合deferred.done() 方法作为一个整体,当所有匹配的元素已经完成各自的动画后,再执行一个回调的动画。

Additional Notes:(其他注意事项:)

  • 所有jQuery效果,包括.fadeIn(),都能通过设置jQuery.fx.off = true全局的关闭,效果等同于持续时间设置为0。更多信息查看 jQuery.fx.off.

示例

实例

淡出所有段落,在600毫秒内完成这些动画。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. span { color:red; cursor:pointer; }
  6. div { margin:3px; width:80px; display:none;
  7. height:80px; float:left; }
  8. div#one { background:#f00; }
  9. div#two { background:#0f0; }
  10. div#three { background:#00f; }
  11. </style>
  12. <script src="http://code.jquery.com/jquery-latest.js"></script>
  13. </head>
  14. <body>
  15. <span>Click here...</span>
  16. <div id="one"></div>
  17. <div id="two"></div>
  18. <div id="three"></div>
  19. <script>
  20. $(document.body).click(function () {
  21. $("div:hidden:first").fadeIn("slow");
  22. });
  23. </script>
  24. </body>
  25. </html>

运行一下

实例

Fades a red block in over the text. Once the animation is done, it quickly fades in more text on top.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. p { position:relative; width:400px; height:90px; }
  6. div { position:absolute; width:400px; height:65px;
  7. font-size:36px; text-align:center;
  8. color:yellow; background:red;
  9. padding-top:25px;
  10. top:0; left:0; display:none; }
  11. span { display:none; }
  12. </style>
  13. <script src="http://code.jquery.com/jquery-latest.js"></script>
  14. </head>
  15. <body>
  16. <p>
  17. Let it be known that the party of the first part
  18. and the party of the second part are henceforth
  19. and hereto directed to assess the allegations
  20. for factual correctness... (<a href="#">click!</a>)
  21. <div><span>CENSORED!</span></div>
  22. </p>
  23. <script>
  24. $("a").click(function () {
  25. $("div").fadeIn(3000, function () {
  26. $("span").fadeIn(100);
  27. });
  28. return false;
  29. });
  30. </script>
  31. </body>
  32. </html>

运行一下


还没有评论.