Tada
The element shrinks a touch as if taking a breath, grows and wiggles side to side, then settles back to its normal size. Made for reveals and “well done” moments.
Badge unlockedSeven days in a row
How to use it
- Paste the snippet just before
</head>. - Add
data-tadato any element. It plays once when it scrolls into view. - Add
data-loopto play it again every so often.
Options
Add any of these attributes to the same element.
data-scale | How big it grows. Default 1.1. |
data-angle | How far each wiggle tilts, in degrees. Default 4. |
data-duration | How long it takes. Default 1100 ms. |
data-delay | Wait before the first tada. Default 0 ms. |
data-loop | Tada again every so often while it’s on screen. |
data-every | Rest between plays, when looping. Default 1600 ms. |
data-hover | Also play it when the pointer moves over the element or it’s tapped. |
Call animacious.tada(el) to play it from your own code, for example when a goal is reached.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Tada: the element shrinks a touch as if taking a breath, grows and
// wiggles side to side, then settles back to its normal size. Good for a
// reveal or a "well done".
// Usage: <div data-tada>…</div>
// Optional: data-scale (how big it grows, default 1.1), data-angle (degrees
// of each wiggle, default 4), data-duration (ms, default 1100), data-delay
// (ms before the first tada), data-loop (tada again every so often while in
// view), data-every (ms of rest between loops, default 1600), data-hover
// (also tada on hover or tap).
// Script use: animacious.tada(el) plays it from code.
var DEFAULTS = { scale: 1.1, angle: 4, 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._tda;
if (s.busy) { if (done) s.after = done; return; }
if (reduced()) { if (done) done(); return; }
s.busy = true;
var d = el.dataset;
function finish() {
s.busy = false;
var after = s.after;
s.after = null;
if (done) done();
if (after) after();
}
var sc = +d.scale || DEFAULTS.scale;
var a = d.angle === undefined ? DEFAULTS.angle : +d.angle;
var low = 1 - (sc - 1);
anime.animate(el, {
keyframes: {
'0%': { scale: 1, rotate: 0 },
'10%': { scale: low, rotate: -a * 0.8 },
'20%': { scale: low, rotate: -a * 0.8 },
'30%': { scale: sc, rotate: a },
'40%': { rotate: -a },
'50%': { rotate: a },
'60%': { rotate: -a },
'70%': { rotate: a * 0.8 },
'80%': { rotate: -a * 0.5 },
'90%': { scale: sc, rotate: a * 0.3 },
'100%': { scale: 1, rotate: 0, ease: 'out(3)' }
},
ease: 'inOut(2)',
duration: +d.duration || DEFAULTS.duration,
onComplete: finish
});
}
function cycle(el) {
var s = el._tda;
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._tda;
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-tada]').forEach(function (el) {
if (el._tda) return;
el._tda = {};
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.tada = function (el) { if (el._tda) play(el); };
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><div class="badge" data-tada>Badge unlocked</div>
<!-- or bigger, repeating: -->
<div class="badge" data-tada data-scale="1.2" data-loop>Badge unlocked</div>