Spin Once
The element winds back a touch, turns one full circle and settles with a tiny overshoot. A playful way to show off a sticker, a badge or a refresh icon.
NewAutumn dropJust landed
How to use it
- Paste the snippet just before
</head>. - Add
data-spin-onceto any element. It spins once when it scrolls into view. - Add
data-loopto spin again every so often.
Options
Add any of these attributes to the same element.
data-turns | How many full turns. Default 1. |
data-direction | cw for clockwise or ccw for anticlockwise. Default cw. |
data-duration | How long the spin takes. Default 1100 ms. |
data-delay | Wait before the first spin. Default 0 ms. |
data-loop | Spin again every so often while it’s on screen. |
data-every | Rest between spins, when looping. Default 1600 ms. |
data-hover | Also play it when the pointer moves over the element or it’s tapped. |
Call animacious.spinOnce(el) to spin it from your own code, for example when a refresh finishes.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Spin Once: the element winds back a touch, turns one full circle and
// settles with a small overshoot.
// Usage: <span data-spin-once>…</span>
// Optional: data-turns (how many full turns, default 1), data-direction
// ("cw", default, or "ccw"), data-duration (ms, default 1100), data-delay
// (ms before the first spin), data-loop (spin again every so often while in
// view), data-every (ms of rest between loops, default 1600), data-hover
// (also spin on hover or tap).
// Script use: animacious.spinOnce(el) spins it from code.
var DEFAULTS = { turns: 1, 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._sno;
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;
anime.utils.set(el, { rotate: 0 });
var after = s.after;
s.after = null;
if (done) done();
if (after) after();
}
var sign = d.direction === 'ccw' ? -1 : 1;
var full = 360 * Math.max(1, +d.turns || DEFAULTS.turns) * sign;
var dur = +d.duration || DEFAULTS.duration;
anime.createTimeline({ onComplete: finish })
// A little wind-up the other way, then the turn with a soft overshoot.
.add(el, { rotate: -14 * sign, scale: 0.94, duration: dur * 0.18, ease: 'out(2)' })
.add(el, { rotate: full + 6 * sign, scale: 1, duration: dur * 0.62, ease: 'inOut(3)' })
.add(el, { rotate: full, duration: dur * 0.2, ease: 'out(2)' });
}
function cycle(el) {
var s = el._sno;
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._sno;
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-spin-once]').forEach(function (el) {
if (el._sno) return;
el._sno = {};
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.spinOnce = function (el) { if (el._sno) play(el); };
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><span class="sticker" data-spin-once>New</span>
<!-- or anticlockwise, every few seconds: -->
<span class="sticker" data-spin-once data-direction="ccw" data-loop>New</span>