jQuery Fade
Fading changes an element's opacity smoothly. Unlike an animated .hide(), a fade does not shrink the element — it simply becomes more or less transparent, which feels softer for messages, images and overlays.
The four fade methods
| Method | What it does |
|---|---|
.fadeIn(duration, easing, callback) |
Fades a hidden element in (opacity 0 → 1), then it is visible |
.fadeOut(duration, easing, callback) |
Fades out (1 → 0), then sets display: none |
.fadeToggle(duration, easing, callback) |
Fades in if hidden, out if visible |
.fadeTo(duration, opacity, easing, callback) |
Fades to a specific opacity (0–1) and stays visible |
duration is milliseconds or "fast"/"slow"; the default is 400 ms. fadeTo requires both the duration and the target opacity.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
.dot { display: inline-block; width: 70px; height: 70px; border-radius: 50%; margin: 8px; }
#d1 { background: crimson; display: none; }
#d2 { background: seagreen; }
#d3 { background: royalblue; }
</style>
</head>
<body>
<button id="in">fadeIn red</button>
<button id="out">fadeOut green</button>
<button id="tog">fadeToggle blue</button>
<br>
<div class="dot" id="d1"></div>
<div class="dot" id="d2"></div>
<div class="dot" id="d3"></div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$("#in").on("click", () => $("#d1").fadeIn());
$("#out").on("click", () => $("#d2").fadeOut("slow"));
$("#tog").on("click", () => $("#d3").fadeToggle(1000));
</script>
</body>
</html>Notice that when the green dot fades out, the blue one moves left at the end: fadeOut finishes with display: none.
fadeTo: partial transparency
fadeTo is useful for "dimming" something without removing it, such as unavailable options or a hover effect on a gallery.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
.tile { display: inline-block; width: 80px; height: 80px; margin: 4px; border-radius: 6px;
color: #fff; font-weight: bold; text-align: center; line-height: 80px; cursor: pointer; }
</style>
</head>
<body>
<p>Hover over a tile: the others dim.</p>
<div id="gallery">
<div class="tile" style="background:#e67e22">A</div>
<div class="tile" style="background:#16a085">B</div>
<div class="tile" style="background:#8e44ad">C</div>
<div class="tile" style="background:#2c3e50">D</div>
</div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$(".tile").on("mouseenter", function () {
$(this).stop().fadeTo(150, 1);
$(".tile").not(this).stop().fadeTo(150, 0.3);
});
$("#gallery").on("mouseleave", function () {
$(".tile").stop().fadeTo(150, 1);
});
</script>
</body>
</html>The .stop() before each fade cancels any fade still in progress, so quick mouse movements do not build up a queue of animations (the stop() lesson later in this section covers it in detail).
Note: After
.fadeTo(…, 0)the element is fully transparent but still takes up space and still receives clicks. Use.fadeOut()if it should disappear from the layout.
A self-closing notification
Fades work nicely for toast-style messages. The callback of fadeIn starts a timer, and fadeOut's callback removes the element from the DOM once it is invisible:
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
#toasts { position: fixed; top: 10px; right: 10px; width: 200px; }
.toast { display: none; background: #333; color: #fff; padding: 8px 12px; border-radius: 6px; margin-bottom: 6px; }
</style>
</head>
<body>
<button id="save">Save</button>
<button id="error">Fail</button>
<div id="toasts"></div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
function toast(message, color) {
const $t = $("<div class='toast'>").text(message).css("background", color);
$("#toasts").append($t);
$t.fadeIn(200, function () {
setTimeout(function () {
$t.fadeOut(600, function () {
$t.remove();
console.log("removed:", message, "| remaining:", $(".toast").length);
});
}, 1500);
});
}
$("#save").on("click", () => toast("Saved ✔", "#2e7d32"));
$("#error").on("click", () => toast("Something went wrong", "#c62828"));
</script>
</body>
</html>Fade vs. CSS transitions
Fades are convenient, but jQuery animates opacity with JavaScript timers. For simple hover effects a CSS transition is lighter:
.tile { transition: opacity .15s; }
#gallery:hover .tile:not(:hover) { opacity: .3; }Use jQuery fades when the effect is triggered by logic (data loaded, a timer, a click elsewhere) or when you need the completion callback.
Test yourself
Which fade method leaves the element visible but semi-transparent?
.fadeOut().fadeTo(400, 0.5).fadeToggle().fadeIn(0.5)
What happens at the end of .fadeOut()?
- Opacity stays at 0 but the element keeps its space
- The element is removed from the DOM
- The element gets
display: none - The element fades back in
Which arguments does .fadeTo() require?
- Only the opacity
- Duration and opacity
- Only a callback
- None