Bounce
The element crouches, jumps and stretches on the way up, then lands with a squash and a little second hop. It’s a cheerful way to say “look at me”.
3
How to use it
- Paste the snippet just before
</head>. - Add
data-bounceto any element. It bounces once when it scrolls into view. - Add
data-loopto keep bouncing every so often.
Options
Add any of these attributes to the same element.
data-height | How high it jumps, in px. Default 40. |
data-duration | How long the whole bounce takes. Default 1100 ms. |
data-delay | Wait before the first bounce. Default 0 ms. |
data-loop | Bounce again every so often while it’s on screen. |
data-every | Rest between bounces, when looping. Default 1600 ms. |
data-hover | Also play it when the pointer moves over the element or it’s tapped. |
Call animacious.bounce(el) to bounce it from your own code, for example when a new message arrives.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Bounce: the element crouches, jumps, stretches on the way up and lands
// with a squash, then gives a little second hop before it settles.
// Usage: <div data-bounce>…</div>
// Optional: data-height (px it jumps, default 40), data-duration (ms for the
// whole bounce, default 1100), data-delay (ms before the first bounce),
// data-loop (bounce again every so often while in view), data-every (ms of
// rest between loops, default 1600), data-hover (also bounce on hover or tap).
// Script use: animacious.bounce(el) bounces it from code.
var DEFAULTS = { height: 40, duration: 1100, delay: 0, every: 1600 };
function on(v) { return v !== undefined && v !== 'false'; }
function reduced() { return window.matchMedia('(prefers-reduced-motion: reduce)').matches; }
function play(el, done) {
var s = el._bnc;
if (s.busy) { if (done) s.after = done; return; }
if (reduced()) { if (done) done(); return; }
s.busy = true;
var d = el.dataset;
var h = d.height === undefined ? DEFAULTS.height : +d.height;
el.style.transformOrigin = '50% 100%'; // squash against the floor
anime.animate(el, {
keyframes: {
'0%': { y: 0, scaleX: 1, scaleY: 1 },
'10%': { y: 0, scaleX: 1.12, scaleY: 0.86, ease: 'out(2)' }, // crouch
'20%': { scaleX: 0.92, scaleY: 1.1, ease: 'out(2)' }, // stretch as it leaves
'38%': { y: -h, scaleX: 1, scaleY: 1, ease: 'out(2)' }, // top of the jump
'58%': { y: 0, scaleX: 0.95, scaleY: 1.06, ease: 'in(2)' }, // falling
'65%': { scaleX: 1.18, scaleY: 0.8, ease: 'out(2)' }, // land: squash
'74%': { y: -h * 0.2, scaleX: 0.98, scaleY: 1.03, ease: 'out(2)' }, // little hop
'86%': { y: 0, scaleX: 1.05, scaleY: 0.95, ease: 'in(2)' },
'100%': { y: 0, scaleX: 1, scaleY: 1, ease: 'out(2)' }
},
duration: +d.duration || DEFAULTS.duration,
onComplete: function () {
s.busy = false;
var after = s.after;
s.after = null;
if (done) done();
if (after) after();
}
});
}
function cycle(el) {
var s = el._bnc;
play(el, function () {
if (!on(el.dataset.loop)) return;
clearTimeout(s.timer);
s.timer = setTimeout(function () {
if (s.visible) cycle(el);
else s.waiting = true;
}, +el.dataset.every || DEFAULTS.every);
});
}
function init() {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (e) {
var s = e.target._bnc;
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]').forEach(function (el) {
if (el._bnc) return;
el._bnc = {};
if (on(el.dataset.hover)) {
el.addEventListener('pointerenter', function () { play(el); });
el.addEventListener('pointerdown', function () { play(el); });
}
if (!reduced()) io.observe(el);
});
}
window.animacious = window.animacious || {};
window.animacious.bounce = function (el) { if (el._bnc) play(el); };
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><span class="app-icon" data-bounce>…</span>
<!-- or bouncing every few seconds, higher: -->
<span class="app-icon" data-bounce data-loop data-height="60">…</span>