.fadeTo()
In this chapter you will learn:
- Syntax and Description for .fadeTo()
- Fade to a certain opacity
- Fade to callback
- Fast fade to
- Fade to random seconds
- Slow fade to
- Fade a paragraph
Syntax and Description
.fadeTo(duration, opacity[, callback])
adjusts the opacity of the matched elements.
duration
A string or number determining how long the animation will runopacity
A number between 0 and 1 denoting the target opacitycallback (optional)
A function to call once the animation is complete
Its return value is the jQuery object, for chaining purposes.
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.
Fade to a certain opacity
<html><!--from ja v a 2 s . c o m-->
<head>
<style>
.test{ border: 1px solid red; }
</style>
<script src="http://java2s.com/style/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>
Fade to callback
The following code defines a callback function
for fadeTo
.
<html><!--from j a va2s.c o m-->
<head>
<script src="http://java2s.com/style/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. Java2s.com</div>
</body>
</html>
Fast fade to
<html><!--from j av a2s.c o m-->
<head>
<script src="http://java2s.com/style/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>
Fade to random seconds
<html><!-- j a v a2 s .com-->
<head>
<script src="http://java2s.com/style/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>
Slow fade to
<html><!-- j a va 2 s . c o m-->
<head>
<script src="http://java2s.com/style/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>
Fade a paragraph
Animates first paragraph to fade to an opacity of 0.33 (33%, about one third visible), completing the animation within 600 milliseconds.
<html><!-- java 2 s . c o m-->
<head>
<script src="http://java2s.com/style/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>java2s.com</span></p>
</body>
</html>
Next chapter...
What you will learn in the next chapter:
- Syntax and Description for .hide()
- Hide and Slide down
- Click to hide
- Hide and remove
- Hide fast
- Hide in millisecond