.fadeTo()

Syntax

.fadeTo(duration, opacity[, callback])

Parameters

duration
A string or number determining how long the animation will run
opacity
A number between 0 and 1 denoting the target opacity
callback (optional)
A function to call once the animation is complete

Return value

The jQuery object, for chaining purposes.

Description

Adjust the opacity of the matched elements.

The .fadeTo() method animates the opacity of the matched elements.

Durations are given in milliseconds; higher values indicate slower animations.

The 'fast' and 'slow' strings can be supplied to indicate durations of 200 and 600 milliseconds, respectively.

If supplied, the callback is fired once the animation is complete.

Examples

Fade to a certain opacity

 
<html>
  <head>
    <style>
      .test{ border: 1px solid red; }
    </style>
    <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
         $("p:parent").fadeTo(1500, 0.3);
    });
    </script>
  </head>
  <body>
      <div><p>paragraph in div</p></div>
      <div>div</div>
  </body>
</html>
  
Click to view the demo

Fade to callback

 
<html>
  <head>
    <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("div").fadeTo(250, Math.random(),function () {
              alert("done");
            });
         
        });
    </script>
  </head>
  <body>
    <body>
          <div>Click me</div>
    </body>
</html>
  
Click to view the demo

Fast fade to

 
<html>
  <head>
    <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                  $("div").fadeTo("fast", Math.random());
        });
    </script>
  </head>
  <body>
    <body>
          <div>Click me</div>
    </body>
</html>
  
Click to view the demo

Fade to random seconds

 
<html>
  <head>
    <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                  $("div").fadeTo("fast", Math.random());
        });
    </script>
  </head>
  <body>
    <body>
          <div>Click me</div>
    </body>
</html>
  
Click to view the demo

Slow fade to

 
<html>
  <head>
    <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                  $("div").fadeTo("slow", 0.33);
        });
    </script>
  </head>
  <body>
    <body>
          <div>Click me</div>
    </body>
</html>
  
Click to view the demo

Animates first paragraph to fade to an opacity of 0.33 (33%, about one third visible), completing the animation within 600 milliseconds.

 
<html>
  <head>
    <script src="http://java2s.com/Book/JavaScriptDemo/jQuery/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("p:first").click(function () {
              $(this).fadeTo("slow", 0.33);
            });
        });
    </script>
  </head>
  <body>
    <body>
      <p><span>asdf</span></p>
    </body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    jQuery  

Effect:
  1. jQuery Effect
  2. .animate()
  3. .css() does animation
  4. .clearQueue()
  5. .delay()
  6. .dequeue()
  7. $.extend()
  8. .fadeIn()
  9. .fadeOut()
  10. .fadeTo()
  11. .hide()
  12. .queue()
  13. .show()
  14. .slideDown()
  15. slideToggle
  16. .slideUp()
  17. .stop()
  18. .toggle()