.fadeOut()

.fadeOut( [ duration ], [ callback ] ) 官网原E文: jQuery

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

  • version added: 1.0.fadeOut( [ duration ], [ callback ] )

    duration一个字符串或者数字决定动画将运行多久。

    callback在动画完成时执行的函数。

  • version added: 1.4.3.fadeOut( [ duration ], [ easing ], [ callback ] )

    duration一个字符串或者数字决定动画将运行多久。

    easing一个用来表示使用哪个缓冲函数来过渡的字符串

    callback在动画完成时执行的函数。

.fadeOut() 方法通过匹配元素的透明度做动画效果。一旦透明度达到0,display样式属性将被设置为none,以确保该元素不再影响页面布局。

持续时间是以毫秒为单位的,数值越大,动画越慢,不是越快。字符串 'fast''slow' 分别代表200和600毫秒的延时。如果提供任何其他字符串,或者这个duration参数被省略,那么默认使用400毫秒的延时。

如果提供回调函数参数,回调函数会在动画完成的时候调用。这个对于将不同的动画串联在一起按顺序排列是非常有用的。这个回调函数不设置任何参数,但是this是存在动画的DOM元素,如果多个元素一起做动画效果,值得注意的是这个回调函数在每个匹配元素上执行一次,不是这个动画作为一个整体。

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

<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').fadeOut('slow', function() {
    // Animation complete.
  });
});

注意:

  • 所有的jQuery效果,包括 .fadeOut(),能使用jQuery.fx.off = true关闭全局性。更多信息请查看jQuery.fx.off

Examples:

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

<!DOCTYPE html>
<html>
<head>
  <style>
  p { font-size:150%; cursor:pointer; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.min.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>

Demo:

Example: 点击淡出在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.min.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>

Demo:

jQuery 1.6 API 中文版Clove整理、修订