.fadeTo()

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

描述: 调整匹配元素的透明度。

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

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

    opacity一个0至1之间表示目标透明度的数字。

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

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

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

    opacity一个0至1之间表示目标透明度的数字。

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

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

.fadeTo() 方法通过匹配元素的不透明度做动画效果。

延时时间是以毫秒为单位的,数值越大,动画越慢,不是越快。字符串 'fast''slow' 分别代表200和600毫秒的延时。如果提供任何其他字符串,或者这个duration参数被省略,那么默认使用400毫秒的延时。和其他效果方法不同,.fadeTo()需要明确的指定duration参数。

如果提供回调函数参数,回调函数会在动画完成的时候调用。这个对于将不同的动画串联在一起按顺序排列是非常有用的。这个回调函数不设置任何参数,但是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 dim it slowly:
  $('#clickme').click(function() {
    $('#book').fadeTo('slow', 0.5, function() {
      // Animation complete.
    });
  });
  

duration设置为0,这个方法只是改变opacity CSS属性,所以.fadeTo(0, opacity).css('opacity', opacity)是一样的效果。

注意:

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

例子:

举例: 将第一个段落的不透明度设置成0.33(33%),在600毫秒内完成这些动画。

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <p>
Click this paragraph to see it fade.
</p>

<p>
Compare to this one that won't fade.
</p>
<script>
$("p:first").click(function () {
$(this).fadeTo("slow", 0.33);
});
</script>

</body>
</html>

Demo:

Example: 点击将div设置成随机的不透明度,在200毫秒内完成这些动画。

<!DOCTYPE html>
<html>
<head>
  <style>
p { width:80px; margin:0; padding:5px; }
div { width:40px; height:40px; position:absolute; }
div#one { top:0; left:0; background:#f00; }
div#two { top:20px; left:20px; background:#0f0; }
div#three { top:40px; left:40px; background:#00f; }
</style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <p>And this is the library that John built...</p>

<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<script>
$("div").click(function () {
$(this).fadeTo("fast", Math.random());
});
</script>

</body>
</html>

Demo:

Example: Find the right answer! The fade will take 250 milliseconds and change various styles when it completes.

<!DOCTYPE html>
<html>
<head>
  <style>
div, p { width:80px; height:40px; top:0; margin:0; 
position:absolute; padding-top:8px; }
p { background:#fcc; text-align:center; }
div { background:blue; }
</style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
  <p>Wrong</p>
<div></div>
<p>Wrong</p>
<div></div>

<p>Right!</p>
<div></div>
<script>
var getPos = function (n) {
return (Math.floor(n) * 90) + "px";
};
$("p").each(function (n) {
var r = Math.floor(Math.random() * 3);
var tmp = $(this).text();
$(this).text($("p:eq(" + r + ")").text());
$("p:eq(" + r + ")").text(tmp);
$(this).css("left", getPos(n));
});
$("div").each(function (n) {
      $(this).css("left", getPos(n));
    })
.css("cursor", "pointer")
.click(function () {
      $(this).fadeTo(250, 0.25, function () {
            $(this).css("cursor", "")
                   .prev().css({"font-weight": "bolder",
                                "font-style": "italic"});
          });
    });

</script>

</body>
</html>

Demo:

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