Drop Bounce In
The element falls in from above, squashes as it lands and bounces twice, each time lower, before it comes to rest, like something dropped onto a table.
Order placedArriving on Thursday
How to use it
- Paste the snippet just before
</head>. - Add
data-drop-bounce-into any element.
Options
Add any of these attributes to the same element.
data-height | Fall this many px instead of from just above the top of its container. |
data-squash | How much it squashes when it lands. 0 for none. Default 0.12. |
data-duration | How long the fall and bounces take. Default 1400 ms. |
data-delay | Wait before the first entrance. Default 0 ms. |
data-loop | Hold, fade away and drop in again. |
data-hold | How long it stays before fading, when looping. Default 1400 ms. |
It falls from just above the top of the nearest container that clips its overflow (overflow: hidden or clip), or of the window. It squashes from its bottom edge, so the script sets transform-origin on the element.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Drop Bounce In: the element falls in from above, squashes as it lands
// and bounces twice, each time lower, before coming to rest.
// Usage: <div data-drop-bounce-in>…</div>
// It falls from just above the top of the nearest container that clips
// its overflow (overflow: hidden or clip), or of the window.
// Optional: data-height (px to fall instead), data-squash (how much it
// squashes on landing, 0 for none, default 0.12), data-duration (ms,
// default 1400), data-delay (ms before the first entrance), data-loop
// (hold, fade away and drop in again), data-hold (ms to stay when
// looping, default 1400).
var DEFAULTS = { squash: 0.12, duration: 1400, delay: 0, hold: 1400 };
function reduced() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
// The box the element falls into.
function clipBox(el) {
for (var p = el.parentElement; p && p !== document.body; p = p.parentElement) {
var s = getComputedStyle(p);
if (s.overflowX !== 'visible' || s.overflowY !== 'visible') return p.getBoundingClientRect();
}
return { left: 0, top: 0, right: window.innerWidth, bottom: window.innerHeight };
}
function enter(el, done) {
var d = el.dataset;
var t = +d.duration || DEFAULTS.duration;
var q = d.squash === undefined ? DEFAULTS.squash : +d.squash || 0;
var h = +d.height || el.getBoundingClientRect().bottom - clipBox(el).top + 8;
var b1 = Math.min(h * 0.2, 90); // first bounce height
var b2 = b1 * 0.3; // second, smaller bounce
el.style.transformOrigin = '50% 100%';
anime.utils.set(el, { y: -h, scaleX: 1, scaleY: 1, opacity: 1 });
anime.createTimeline({ onComplete: done })
// Fall, speeding up like a dropped object, and squash on landing.
.add(el, { y: 0, duration: t * 0.38, ease: 'in(2)' }, 0)
.add(el, { scaleY: 1 - q, scaleX: 1 + q * 0.6, duration: t * 0.05, ease: 'out(2)' }, t * 0.38)
// First bounce: spring back up, stretching a touch as it goes.
.add(el, { y: -b1, duration: t * 0.15, ease: 'out(2)' }, t * 0.43)
.add(el, { scaleY: 1 + q * 0.25, scaleX: 1 - q * 0.15, duration: t * 0.07, ease: 'out(2)' }, t * 0.43)
.add(el, { scaleY: 1, scaleX: 1, duration: t * 0.08, ease: 'inOut(2)' }, t * 0.5)
.add(el, { y: 0, duration: t * 0.15, ease: 'in(2)' }, t * 0.58)
.add(el, { scaleY: 1 - q * 0.5, scaleX: 1 + q * 0.3, duration: t * 0.04, ease: 'out(2)' }, t * 0.73)
// Second, smaller bounce, then settle.
.add(el, { y: -b2, duration: t * 0.09, ease: 'out(2)' }, t * 0.77)
.add(el, { scaleY: 1, scaleX: 1, duration: t * 0.08, ease: 'out(2)' }, t * 0.77)
.add(el, { y: 0, duration: t * 0.09, ease: 'in(2)' }, t * 0.86)
.add(el, { scaleY: 1 - q * 0.2, scaleX: 1 + q * 0.1, duration: t * 0.02, ease: 'out(2)' }, t * 0.95)
.add(el, { scaleY: 1, scaleX: 1, duration: t * 0.03, ease: 'out(2)' }, t * 0.97);
}
function cycle(el) {
var d = el.dataset;
var s = el._dbi;
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._dbi;
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-drop-bounce-in]').forEach(function (el) {
if (el._dbi) return;
el._dbi = { 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="card" data-drop-bounce-in>Order placed</div>
<!-- or a short 80px hop with no squash: -->
<div class="card" data-drop-bounce-in data-height="80" data-squash="0">Order placed</div>