• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • fadeTo()

    调整匹配元素的透明度。

    .fadeTo(duration, opacity[, complete ])
    • duration类型: String,Number。一个字符串或者数字决定动画将运行多久。
    • opacity类型: Number。0和1之间的数字表示目标元素的不透明度。
    • complete类型:Function()。在动画完成时执行的函数。
    .fadeTo(duration, opacity[, easing ][, complete ])
    • duration类型: String,Number。一个字符串或者数字决定动画将运行多久。
    • opacity类型: Number。0和1之间的数字表示目标元素的不透明度。
    • easing类型: String。一个字符串,表示动画过渡使用哪种缓动函数。(译者注:jQuery自身提供"linear"和"swing",其他可以使用相关的插件)
    • complete类型:Function()。在动画完成时执行的函数。

    .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.
        });
      });
    
    Click here

    jquery图片

    picture

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

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

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

    例子

    把第一个段落的透明度渐变成 0.33(33%,大约三分之一透明度),用时 600 毫秒。

    <!DOCTYPE html>
    <html>
    <head>
      <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.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>
    

    Click this paragraph to see it fade.

    Compare to this one that won't fade.

    每次点击后把 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="https://www.lanmper.cn/static/js/jquery-3.5.0.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>
    

    And this is the library that John built...

    找到正确答案!渐变耗时 250 毫秒,并且在完成后改变字体样式。

    <!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="https://www.lanmper.cn/static/js/jquery-3.5.0.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>
    

    Wrong

    Wrong

    Right!

    上篇:fadeOut()

    下篇:fadeToggle()