...

Library - JQuery

Back to Course

Lesson Description


Lession - #560 JQuery Fade


With jQuery you can fade elements in and out of visibility.

jQuery fading methods

With jQuery you can fade an element in and out of visibility.

jQuery has the following fade methods:
  • fadeIn(>
  • fadeOut(>
  • fadeToggle(>
  • fadeTo(>


    jQuery fadeIn(>
    method

    The jQuery fadeIn(>
    method is used to fade in a hidden element.

    Syntax:
    
    $(selector>
    .fadeIn(speed,callback>
    ;


    The optional speed parameter specifies the duration of the effect. It will take the following values: "slow", "fast", or milliseconds.

    The optional callback parameter is a function to be dead after the fading completes.

    jQuery fadeOut(>
    method

    The jQuery fadeOut(>
    method is used to fade out a visible element.

    Syntax:
    
    $(selector>
    .fadeOut(speed,callback>
    ;
    The optional speed parameter specifies the duration of the effect. It will take the following values: "slow", "fast", or milliseconds.

    The optional callback parameter is a function to be executed after the fading completes.

    jQuery fadeToggle(>
    method

    The jQuery fadeToggle(>
    method toggles between the fadeIn(>
    and fadeOut(>
    methods.

    If the elements are faded out, fadeToggle(>
    will fade them in.

    If the elements are faded in, fadeToggle(>
    will fade them out.

    Syntax:
    
    $(selector>
    .fadeToggle(speed,callback>
    ;


    The optional speed parameter specifies the duration of the effect. It can take the following values: "slow", "fast", or milliseconds.

    The optional callback parameter is a function to be executed after the fading completes.

    jQuery fadeTo(>
    method

    The jQuery fadeTo(>
    method allows fading to a given opacity (value between 0 and 1>
    .

    Syntax:
    
    $(selector>
    .fadeTo(speed,opacity,callback>
    ;


    The required speed parameter specifies the duration of the effect. It will take the subsequent values: "slow", "fast", or milliseconds.

    The required opacity parameter in the fadeTo(>
    method specifies fading to a given opacity (value between zero and 1>
    .

    The optional callback parameter is a function to be executed when the function completes.

    Example
    
    
    <html>  
    <head> 
     <title>jQuery Example</title>  
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">  
     </script>  
       
    
     <script type="text/javascript" language="javascript">  
     
          $(document>
    .ready(function(>
    { $("button">
    .click(function(>
    { $("#div1">
    .fadeTo("slow", 0.15>
    ; }>
    ; }>
    ; $(document>
    .ready(function(>
    { $("button">
    .click(function(>
    { $("#div1">
    .fadeToggle(>
    ; }>
    ; }>
    ; </script> </head> <body> <p> Demonstrate fadeTo(>
    with different parameters.</p> <button> Click to fade boxes</button> <br> <br> <div id="div1" style="width:80px;height:80px;background-color:green;"> </div> <br> <p> Demonstrate fadeToggle(>
    with different speed parameters.</p> <button> Click to fade in/out boxes</button> <br> <br> <div id="div1" style="width:80px;height:80px;background-color:yellow;"> </div> <br> </body> </html>
    Try it here