Character Skew-in
Letters slide in from the right leaning hard, like speeding italics, then whip upright as they land, rocking a touch past straight before they settle.
Lean into it
How to use it
- Paste the snippet just before
</head>. - Add
data-character-skew-into any heading or text element.
Options
Add any of these attributes to the same element.
data-loop | Slide in, hold, slide away to the left and in again. |
data-hold | How long the text stays before leaving, when looping. Default 1600 ms. |
data-skew | How hard the letters lean at the start, in degrees. Default 40. |
data-distance | How far the letters slide, in em. Default 1.2. |
data-duration | How long each letter takes to land. Default 1000 ms. |
data-stagger | Gap between letters. Default 40 ms. |
data-delay | Wait before the first letter. Default 0 ms. |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Character Skew-in: letters slide in from the right leaning hard, then
// snap upright as they land, overshooting the lean a little the other way.
// Usage: <h1 data-character-skew-in>Lean into it</h1>
// Optional: data-loop (letters lean away to the left and slide in again),
// data-hold (ms to stay before leaving), data-skew (degrees of lean at the
// start; default 40), data-distance (how far they slide, in em; default
// 1.2), data-duration (ms per letter), data-stagger (ms between letters),
// data-delay (ms before starting).
var DEFAULTS = { skew: 40, distance: 1.2, duration: 1000, stagger: 40, delay: 0, hold: 1600 };
var css =
'.csk-word{display:inline-block;white-space:nowrap}' +
'.csk-char{display:inline-block;transform-origin:50% 100%;will-change:transform}';
var style = document.createElement('style');
style.textContent = css;
document.head.appendChild(style);
function reduced() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}
function split(el) {
var text = el.textContent.replace(/\s+/g, ' ').trim();
el.setAttribute('aria-label', text);
el.textContent = '';
var chars = [];
text.split(' ').forEach(function (word, i) {
if (i) el.appendChild(document.createTextNode(' '));
var w = document.createElement('span');
w.className = 'csk-word';
w.setAttribute('aria-hidden', 'true');
Array.from(word).forEach(function (c) {
var s = document.createElement('span');
s.className = 'csk-char';
s.textContent = c;
w.appendChild(s);
chars.push(s);
});
el.appendChild(w);
});
return chars;
}
function play(el, chars) {
var d = el.dataset;
var loop = d.loop !== undefined && d.loop !== 'false';
var duration = +d.duration || DEFAULTS.duration;
var stagger = +d.stagger || DEFAULTS.stagger;
var skew = +d.skew || DEFAULTS.skew;
var distance = +d.distance || DEFAULTS.distance;
var tl = anime.createTimeline({ loop: loop, loopDelay: 300, delay: +d.delay || DEFAULTS.delay });
tl.add(chars, {
// Slanted like italics while moving, the tops trailing behind, then
// whipped upright (and a touch past) as they land.
x: { from: distance + 'em', to: '0em', ease: 'outExpo' },
skewX: { from: -skew, to: 0, ease: 'outBack(2.2)' },
opacity: { from: 0, to: 1, duration: duration * 0.35, ease: 'linear' },
duration: duration,
delay: anime.stagger(stagger)
});
if (loop) {
tl.add(chars, {
x: -distance + 'em',
skewX: -skew,
opacity: 0,
duration: duration * 0.5,
ease: 'in(3)',
delay: anime.stagger(stagger * 0.6)
}, '+=' + (+d.hold || DEFAULTS.hold));
}
}
function init() {
var io = new IntersectionObserver(function (entries) {
entries.forEach(function (e) {
if (!e.isIntersecting) return;
io.unobserve(e.target);
play(e.target, e.target._cskChars);
});
}, { threshold: 0.2 });
document.querySelectorAll('[data-character-skew-in]').forEach(function (el) {
if (el._cskChars) return;
var chars = split(el);
el._cskChars = chars;
if (reduced()) return;
anime.utils.set(chars, { opacity: 0 });
io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><h1 data-character-skew-in>Lean into it</h1>
<!-- or, leaning harder and looping: -->
<h1 data-character-skew-in data-skew="55" data-loop>Lean into it</h1>