Fade effects change an element's opacity smoothly.
| Method | Effect |
|---|---|
.fadeIn() |
fade a hidden element in |
.fadeOut() |
fade out, then hide |
.fadeToggle() |
fade in or out |
.fadeTo(speed, opacity) |
fade to a set opacity, e.g. 0.4 |
All accept a speed ('slow', 'fast', ms) and a callback.
Delay
.delay(2000) waits before the next effect in the queue:
$('#msg').fadeIn().delay(2000).fadeOut();
Perfect for "Saved!" messages that disappear by themselves.
Example
Example — Auto-hiding notification
HTML
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<button id="save">Save profile</button>
<div id="toast" style="display:none;margin-top:10px;padding:10px 14px;background:#2b8a3e;color:#fff;border-radius:6px;width:max-content">✓ Profile saved</div>
<p style="margin-top:24px">
<img id="pic" src="https://picsum.photos/id/1025/160/110" alt="Dog" style="border-radius:8px">
</p>
<button id="dim">Dim image</button>
<button id="full">Full image</button>
<script>
$(function () {
$('#save').click(function () {
$('#toast').stop(true, true).fadeIn(300).delay(2000).fadeOut(600);
});
$('#dim').click(() => $('#pic').fadeTo('slow', 0.3));
$('#full').click(() => $('#pic').fadeTo('slow', 1));
});
</script>