Word Slide-up
Whole words rise from behind an invisible mask, one after another, shooting up fast and gliding to a stop. Loop it and they carry on up and out before the next rise.
Every word on cue
How to use it
- Paste the snippet just before
</head>. - Add
data-word-slide-upto any heading or text element.
Options
Add any of these attributes to the same element.
data-loop | Rise in, hold, slide on up and out, and rise again. |
data-hold | How long the text stays before leaving, when looping. Default 1800 ms. |
data-duration | How long each word takes to rise. Default 1100 ms. |
data-stagger | Gap between words. Default 90 ms. |
data-delay | Wait before the first word. Default 0 ms. |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bundles/anime.umd.min.js"></script>
<script>
(function () {
// Word Slide-up: whole words rise from behind an invisible mask, one after
// another, fast at first and gliding to a stop.
// Usage: <h1 data-word-slide-up>Every word on cue</h1>
// Optional: data-loop (words slide on up and out, then rise again),
// data-hold (ms to stay before leaving), data-duration (ms per word),
// data-stagger (ms between words), data-delay (ms before starting).
var DEFAULTS = { duration: 1100, stagger: 90, delay: 0, hold: 1800 };
var css =
// The mask: each word clips its own inner span, with room for descenders.
'.wsu-mask{display:inline-block;white-space:nowrap;clip-path:inset(-0.05em -0.15em -0.2em -0.15em)}' +
'.wsu-word{display:inline-block;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 = '';
return text.split(' ').map(function (word, i) {
if (i) el.appendChild(document.createTextNode(' '));
var mask = document.createElement('span');
mask.className = 'wsu-mask';
mask.setAttribute('aria-hidden', 'true');
var w = document.createElement('span');
w.className = 'wsu-word';
w.textContent = word;
mask.appendChild(w);
el.appendChild(mask);
return w;
});
}
function play(el, words) {
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 tl = anime.createTimeline({ loop: loop, loopDelay: 200, delay: +d.delay || DEFAULTS.delay });
tl.add(words, {
y: ['130%', '0%'],
duration: duration,
ease: 'outExpo',
delay: anime.stagger(stagger)
});
if (loop) {
// Carry on upwards, so the next rise reads as a fresh line.
tl.add(words, {
y: ['0%', '-130%'],
duration: duration * 0.55,
ease: 'inQuart',
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._wsuWords);
});
}, { threshold: 0.2 });
document.querySelectorAll('[data-word-slide-up]').forEach(function (el) {
if (el._wsuWords) return;
var words = split(el);
el._wsuWords = words;
if (reduced()) return;
words.forEach(function (w) { w.style.transform = 'translateY(130%)'; });
io.observe(el);
});
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();
</script><h1 data-word-slide-up>Every word on cue</h1>
<!-- or, looping: -->
<h1 data-word-slide-up data-loop>Every word on cue</h1>