Wobble
The element sways side to side, tilting as it goes, each sway smaller than the last until it stops. Handy for “not quite” moments, like a wrong password.
Password•••••••Wrong password. Try again.
How to use it
- Paste the snippet just before
</head>. - Add
data-wobbleto any element. It wobbles once when it scrolls into view. - Add
data-loopto wobble again every so often.
Options
Add any of these attributes to the same element.
data-distance | How far the first sway goes, in px. Default 22. |
data-angle | How far the first sway tilts, in degrees. Default 5. |
data-duration | How long it takes. Default 1000 ms. |
data-delay | Wait before the first wobble. Default 0 ms. |
data-loop | Wobble again every so often while it’s on screen. |
data-every | Rest between wobbles, when looping. Default 1600 ms. |
data-hover | Also play it when the pointer moves over the element or it’s tapped. |
Call animacious.wobble(el) to wobble it from your own code, for example when a sign-in fails.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Wobble: the element sways side to side, tilting as it goes, each sway
// smaller than the last until it stops. Handy for "not quite" moments,
// like a wrong password.
// Usage: <div data-wobble>…</div>
// Optional: data-distance (px of the first sway, default 22), data-angle
// (degrees of the first tilt, default 5), data-duration (ms, default 1000),
// data-delay (ms before the first wobble), data-loop (wobble again every so
// often while in view), data-every (ms of rest between loops, default
// 1600), data-hover (also wobble on hover or tap).
// Script use: animacious.wobble(el) wobbles it from code.
var DEFAULTS = { distance: 22, angle: 5, duration: 1000, 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._wbl;
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 x = d.distance === undefined ? DEFAULTS.distance : +d.distance;
var a = d.angle === undefined ? DEFAULTS.angle : +d.angle;
anime.animate(el, {
keyframes: {
'0%': { x: 0, rotate: 0 },
'15%': { x: -x, rotate: -a },
'30%': { x: x * 0.8, rotate: a * 0.6 },
'45%': { x: -x * 0.6, rotate: -a * 0.6 },
'60%': { x: x * 0.4, rotate: a * 0.4 },
'75%': { x: -x * 0.2, rotate: -a * 0.2 },
'100%': { x: 0, rotate: 0 }
},
ease: 'inOut(2)',
duration: +d.duration || DEFAULTS.duration,
onComplete: finish
});
}
function cycle(el) {
var s = el._wbl;
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._wbl;
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-wobble]').forEach(function (el) {
if (el._wbl) return;
el._wbl = {};
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.wobble = function (el) { if (el._wbl) play(el); };
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><div class="field" data-wobble>…</div>
<!-- or from code, when the password is wrong: -->
<script>animacious.wobble(document.querySelector('.field'));</script>