01 · HTML
<h1 data-color-letters>Hello world</h1>
@andresclua/color-letters
Splits your text with Splitting.js, gives every letter a class and a --color-letters variable. CSS does the rest. Move the pointer over the title.
--color-letters, changed with setColor()Quick start
01 · HTML
<h1 data-color-letters>Hello world</h1>
02 · JS
import ColorLetters from '@andresclua/color-letters';
new ColorLetters({ color: '#ff4d6d' });
03 · CSS
.c--char { transition: color 1s; }
.c--char:hover {
color: var(--color-letters);
transition: none;
}
Result
Hello world
Bonus · page transitions
Click Examples or API in the menu. The pages are plain HTML; Pagefold swaps them without a reload, and GSAP throws the title's letters away in their palette colors and builds the next title from below.
Split every title with a palette
import ColorLetters from '@andresclua/color-letters';
const palette = ['#ff4d6d', '#ffd166', '#06d6a0', '#4cc9f0', '#9b5de5'];
// run on load and after every swap
const init = () => new ColorLetters({ element: '[data-title]', colors: palette });
Animate the letters with Pagefold + GSAP
import Pagefold from 'pagefold';
import PagefoldJs from 'pagefold/js';
import PagefoldA11y from 'pagefold/a11y';
import { gsap } from 'gsap';
const chars = () => document.querySelectorAll('#pagefold [data-title] .char');
const color = (i, el) => getComputedStyle(el).getPropertyValue('--char-color');
new Pagefold({
animationSelector: false,
plugins: [new PagefoldA11y(), new PagefoldJs({ animations: [{
out: () => gsap.to(chars(), {
color, y: () => innerHeight, rotation: () => gsap.utils.random(-140, 140),
stagger: { amount: 0.2, from: 'random' }, ease: 'power2.in',
}),
in: () => gsap.from(chars(), {
color, yPercent: 200, opacity: 0,
stagger: { amount: 0.25, from: 'random' }, ease: 'back.out(1.6)',
}),
}] })],
hooks: { 'content:replace': init },
});