The fadeTo() method is an inbuilt method in jQuery that is used to change the opacity of the selected element.
Syntax:
$(selector).fadeTo(speed, opacity, easing, call_function)
Here selector is the selected element.
Parameter: It accepts four parameters which are specified below-
- speed: It specifies the speed of the fading effect.
- opacity: It specifies to fade and this value should be between 0.0 and 1.0.
- easing: It specifies the speed at different points of animation.
- call_function: It is an optional parameter and performs a function after performing fadeTo method.
Return Value: It does not return any value.
Example 1: In the below code, no optional function parameter is passed.
HTML
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
< style >
body {
width: 40%;
height: 100px;
border: 2px solid green;
padding: 20px;
}
</ style >
</ head >
< body >
< p >
This paragraph will fade out !
</ p >
< p >
This paragraph will not fade !
</ p >
< script >
$("p:first").click(function () {
$(this).fadeTo("slow", 0.33);
});
</ script >
</ body >
</ html >
|
Output:
Example 2: In the below code, an optional function parameter is passed.
HTML
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
< script >
$(document).ready(function () {
$("button").click(function () {
$("p").fadeTo(2000, 0.2, function () {
alert("The fadeTo effect has finished!");
});
});
});
</ script >
< style >
body {
width: 40%;
height: 100px;
border: 2px solid green;
padding: 20px;
}
</ style >
</ head >
< body >
< p >This is a paragraph.</ p >
< button >Click me</ button >
</ body >
</ html >
|
Output:
Last Updated :
03 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...