Jack-in-the-box
The element springs up from a squashed state at its base, like a toy popping out of its box. It shoots past full height, bounces back and rocks side to side before it settles.
Surprise!You’ve got a gift card
How to use it
- Paste the snippet just before
</head>. - Add
data-jack-in-the-boxto any element.
Options
Add any of these attributes to the same element.
data-wobble | How far it rocks from side to side, in degrees. 0 for none. Default 12. |
data-duration | How long the whole entrance takes. Default 1200 ms. |
data-delay | Wait before the first entrance. Default 0 ms. |
data-loop | Hold, fade away and spring up again. |
data-hold | How long it stays before fading, when looping. Default 1400 ms. |
It springs 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 () {
// Jack-in-the-box: the element springs up from a squashed state at its
// base, overshoots, and wobbles side to side before it settles upright.
// Usage: <div data-jack-in-the-box>…</div>
// Optional: data-wobble (how far it rocks, in degrees, default 12),
// data-duration (ms, default 1200), data-delay (ms before the first
// entrance), data-loop (hold, fade away and spring up again), data-hold
// (ms to stay when looping, default 1400).
var DEFAULTS = { wobble: 12, duration: 1200, delay: 0, hold: 1400 };
function reduced() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
function enter(el, done) {
var d = el.dataset;
var t = +d.duration || DEFAULTS.duration;
var w = d.wobble === undefined ? DEFAULTS.wobble : +d.wobble || 0;
// It springs from its base, like a toy out of its box.
el.style.transformOrigin = '50% 100%';
anime.utils.set(el, { scaleX: 0.6, scaleY: 0.05, rotate: 0, opacity: 0 });
anime.createTimeline({ onComplete: done })
.add(el, { opacity: { to: 1, duration: t * 0.12, ease: 'linear' } }, 0)
// Shoot up past full height, then spring back to it.
.add(el, {
scaleY: [
{ to: 1.18, duration: t * 0.22, ease: 'out(3)' },
{ to: 0.92, duration: t * 0.16, ease: 'inOut(2)' },
{ to: 1.04, duration: t * 0.14, ease: 'inOut(2)' },
{ to: 1, duration: t * 0.18, ease: 'out(2)' }
],
scaleX: [
{ to: 0.9, duration: t * 0.22, ease: 'out(3)' },
{ to: 1.06, duration: t * 0.16, ease: 'inOut(2)' },
{ to: 0.98, duration: t * 0.14, ease: 'inOut(2)' },
{ to: 1, duration: t * 0.18, ease: 'out(2)' }
]
}, 0)
// Rock side to side on its base, each swing smaller.
.add(el, {
rotate: [
{ to: -w, duration: t * 0.2, ease: 'out(2)' },
{ to: w * 0.6, duration: t * 0.2, ease: 'inOut(2)' },
{ to: -w * 0.3, duration: t * 0.18, ease: 'inOut(2)' },
{ to: w * 0.1, duration: t * 0.16, ease: 'inOut(2)' },
{ to: 0, duration: t * 0.14, ease: 'out(2)' }
]
}, t * 0.12);
}
function cycle(el) {
var d = el.dataset;
var s = el._jib;
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._jib;
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-jack-in-the-box]').forEach(function (el) {
if (el._jib) return;
el._jib = {};
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-jack-in-the-box>Surprise!</div>
<!-- or a calmer spring with less rocking: -->
<div class="card" data-jack-in-the-box data-wobble="5">Surprise!</div>