Bounce In
The element pops in from small with a springy bounce: it overshoots, wobbles back and settles at full size. Turn the springiness up or down to suit.
Achievement unlockedSeven-day streak
How to use it
- Paste the snippet just before
</head>. - Add
data-bounce-into any element.
Options
Add any of these attributes to the same element.
data-scale | The size it starts at. Default 0.3. |
data-bounce | How springy, from 0 (one soft overshoot) to 1 (lively wobbles). Default 0.5. |
data-duration | How long the bounce takes. Default 1100 ms. |
data-delay | Wait before the first entrance. Default 0 ms. |
data-loop | Hold, fade away and pop in again. |
data-hold | How long it stays before fading, when looping. Default 1400 ms. |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Bounce In: the element pops in from small with a springy scale bounce,
// overshooting and wobbling a little before it settles at full size.
// Usage: <div data-bounce-in>…</div>
// Optional: data-scale (the size it starts at, default 0.3), data-bounce
// (how springy, 0 to 1, default 0.5), data-duration (ms, default 1100),
// data-delay (ms before the first entrance), data-loop (hold, fade away
// and pop in again), data-hold (ms to stay when looping, default 1400).
var DEFAULTS = { scale: 0.3, bounce: 0.5, duration: 1100, delay: 0, hold: 1400 };
function reduced() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
function enter(el, done) {
var d = el.dataset;
var duration = +d.duration || DEFAULTS.duration;
var bounce = d.bounce === undefined ? DEFAULTS.bounce : Math.min(1, Math.max(0, +d.bounce || 0));
// A shorter elastic period means quicker, livelier wobbles.
var period = (0.9 - bounce * 0.6).toFixed(2);
anime.utils.set(el, { scale: d.scale === undefined ? DEFAULTS.scale : +d.scale, opacity: 0 });
anime.animate(el, {
scale: { to: 1, ease: 'outElastic(1, ' + period + ')' },
opacity: { to: 1, duration: duration * 0.2, ease: 'linear' },
duration: duration,
onComplete: done
});
}
function cycle(el) {
var d = el.dataset;
var s = el._bni;
enter(el, function () {
if (d.loop === undefined || d.loop === 'false') return;
setTimeout(function () {
anime.animate(el, {
opacity: 0,
duration: 400,
ease: 'out(2)',
onComplete: function () {
// Wait until it's back on screen before the next entrance.
setTimeout(function () {
if (s.visible) cycle(el);
else s.waiting = true;
}, 500);
}
});
}, +d.hold || DEFAULTS.hold);
});
}
function init() {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (e) {
var s = e.target._bni;
s.visible = e.isIntersecting;
if (!e.isIntersecting) return;
if (!s.started) {
s.started = true;
setTimeout(function () { cycle(e.target); }, +e.target.dataset.delay || DEFAULTS.delay);
} else if (s.waiting) {
s.waiting = false;
cycle(e.target);
}
});
}, { threshold: 0.2 });
document.querySelectorAll('[data-bounce-in]').forEach(function (el) {
if (el._bni) return;
el._bni = { n: 0 };
if (reduced()) return;
anime.utils.set(el, { opacity: 0 });
io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><div class="badge" data-bounce-in>Achievement unlocked</div>
<!-- or extra springy: -->
<div class="badge" data-bounce-in data-bounce="0.9">Achievement unlocked</div>