jQuery fadeTo()

Introduction

Gradually change the opacity of all <p> elements:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*w w  w  .j  a  v a 2  s.com*/
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").fadeTo(1000, 0.4);
  });
});
</script>
</head>
<body>

<button>Gradually change the opacity of the p element</button>

<p>This is a paragraph.</p>

</body>
</html>

The fadeTo() method gradually changes the opacity for selected elements to a specified opacity.

$(selector).fadeTo(speed,opacity,easing,callback)
Parameter
Optional
Description
speed




Required.




the speed of the fading effect.
Possible values:
milliseconds
"slow"
"fast"
opacity

Required.

the opacity to fade to.
Must be a number between 0.00 and 1.00
easing




Optional.




the speed of the element in different points of the animation.
Default value is "swing"
Possible values:
"swing" - moves slower at the beginning/end, faster in the middle
"linear" - moves in a constant speed
callback
Optional.
A function to be executed after the fadeTo() method is completed



PreviousNext

Related