Bounce Out
The element gives a little squeeze, swells slightly as if taking a breath, then shrinks away and fades to nothing.
Item removedWireless headphones
How to use it
- Paste the snippet just before
</head>. - Add
data-bounce-outto any element.
Options
Add any of these attributes to the same element.
data-swell | The size it swells to before it shrinks. Default 1.1. |
data-scale | The size it shrinks to as it vanishes. Default 0.3. |
data-duration | How long it takes to leave. Default 750 ms. |
data-delay | How long it stays in view before it leaves. Default 1200 ms. |
data-loop | Fade back in, hold and bounce out again. |
data-hold | How long it stays between exits, when looping. Default 1400 ms. |
It plays once it has been in view for data-delay, and ends hidden (visibility: hidden) but still taking up its space. To play it when you choose, such as when something is dismissed, leave the attribute off and call animacious.bounceOut(el).
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Bounce Out: the element gives a little squeeze, swells slightly, then
// shrinks away and fades out.
// Usage: <div data-bounce-out>…</div>
// It plays once the element has been in view for data-delay ms, and ends
// hidden (visibility: hidden) but still taking up its space.
// Optional: data-swell (the size it swells to, default 1.1), data-scale
// (the size it shrinks to as it vanishes, default 0.3),
// data-duration (ms, default 750), data-delay (ms in view before it
// leaves, default 1200), data-loop (fade back in, hold and bounce out
// again), data-hold (ms to stay between exits when looping, default 1400).
// Script use: animacious.bounceOut(el, done) plays it on any element now,
// using its data-* options, and calls done afterwards.
var DEFAULTS = { swell: 1.1, scale: 0.3, duration: 750, delay: 1200, hold: 1400 };
function reduced() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
function leave(el, done) {
var d = el.dataset;
var t = +d.duration || DEFAULTS.duration;
if (reduced()) {
anime.utils.set(el, { opacity: 0 });
if (done) done();
return;
}
var swell = +d.swell || DEFAULTS.swell;
var end = d.scale === undefined ? DEFAULTS.scale : +d.scale;
anime.animate(el, {
// Gather, swell, then drop away.
scale: [
{ to: 1 - (swell - 1) * 0.8, duration: t * 0.2, ease: 'inOut(2)' },
{ to: swell, duration: t * 0.3, ease: 'out(2)' },
{ to: end, duration: t * 0.5, ease: 'in(3)' }
],
opacity: { to: 0, delay: t * 0.55, duration: t * 0.45, ease: 'in(2)' },
onComplete: function () {
if (done) done();
}
});
}
function cycle(el) {
var d = el.dataset;
var s = el._bno;
leave(el, function () {
if (d.loop === undefined || d.loop === 'false') {
el.style.visibility = 'hidden';
return;
}
// Come back quietly in place, then go again once it's on screen.
setTimeout(function () {
anime.utils.set(el, { scale: 1, opacity: 0 });
anime.animate(el, {
opacity: 1,
duration: 400,
ease: 'out(2)',
onComplete: function () {
setTimeout(function () {
if (s.visible) cycle(el);
else s.waiting = true;
}, +d.hold || DEFAULTS.hold);
}
});
}, 400);
});
}
window.animacious = window.animacious || {};
window.animacious.bounceOut = function (el, done) {
leave(el, function () {
el.style.visibility = 'hidden';
if (done) done(el);
});
};
function init() {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (e) {
var s = e.target._bno;
s.visible = e.isIntersecting;
if (!e.isIntersecting) return;
if (!s.started) {
s.started = true;
var wait = e.target.dataset.delay;
setTimeout(function () { cycle(e.target); }, wait === undefined ? DEFAULTS.delay : +wait || 0);
} else if (s.waiting) {
s.waiting = false;
cycle(e.target);
}
});
}, { threshold: 0.2 });
document.querySelectorAll('[data-bounce-out]').forEach(function (el) {
if (el._bno) return;
el._bno = { n: 0 };
if (!reduced()) io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><div class="card" data-bounce-out>Item removed</div>
<!-- or on demand, removing it afterwards: -->
<script>
animacious.bounceOut(card, function () { card.remove(); });
</script>