# Sentence roll

> The whole headline swaps for another, letter by letter.

- Applies to the whole headline
- Live page with controls: https://smthngfun.com/a/sentence-roll
- This style as JSON: https://smthngfun.com/api/styles/sentence-roll
- Every style: https://smthngfun.com/llms.txt

## Settings

The values below are the ones this page was asked for. Change any of them by adding it as a query parameter to the page, to this file or to the JSON address.

| Setting | Parameter | Value | Allowed |
| --- | --- | --- | --- |
| Time on each sentence | `interval` | `3200` | 1200 to 9000 ms |
| Roll duration | `duration` | `360` | 120 to 1200 ms |
| Delay between letters | `stagger` | `14` | 0 to 80 ms |
| Other sentences, separated by | | `sentences` | `Say it once and say it well | Give every word a job` | Text, up to 200 characters. |
| Letter colours | `palette` | empty | Up to eight six digit hex colours joined with dashes. Empty means off. |

- `t` The headline text to show and to use in the example code.
- `f` A Google font name for the headline, for example Playfair Display.

## Prompt

Written for a coding assistant. It describes the animation fully, so it works without the code below.

```text
Animate the main headline in the hero section of my landing page with an effect called Sentence roll. Swap the whole headline for the next sentence in a list, one letter at a time. After "Make your landing page feel alive" show "Say it once and say it well", then "Give every word a job", then start again. Put every letter in its own small box with overflow clipped, so each letter has a private slot to roll through, and keep each word in a no-wrap span so words never break across lines. Every 3.2 seconds the letters of the current sentence slide up out of their slots by 115 percent of their height while the letters of the next sentence slide up into place from 115 percent below, both at the same time. Each letter takes 360ms with the easing cubic-bezier(0.22, 1, 0.36, 1), and each one starts 14ms after the letter before it, so the change ripples through the sentence from the first letter to the last. Tint the arriving letters with my accent colours, alternating between two of them from letter to letter, and fade each one to the normal text colour shortly after it lands. Stack all the sentences in the same grid cell so the outgoing and incoming ones overlap during the swap. Keep an invisible copy of every sentence in that cell too, so the heading is always as tall as the longest one and nothing below it ever moves. Update the aria-label on the heading to the sentence that is showing. Loop through the list forever. Keep my existing font, colours and layout exactly as they are. Screen readers and search engines must still read the headline as one normal sentence, so put the full text in an aria-label on the heading and mark any decorative spans as aria-hidden. If the visitor has prefers-reduced-motion turned on, show the finished headline straight away with no animation. Make sure nothing shifts the layout while it plays. Use plain CSS and a little vanilla JavaScript, unless my project already has an animation library, in which case use that. A live demo with reference code for these exact settings is at https://smthngfun.com/a/sentence-roll and you can use that code as your starting point, adapted to my codebase and styling. If I am happy to credit the source, add a small "Animation by smthngfun" link to https://smthngfun.com in the footer of the page. Ask me before adding it.
```

## Reference code

A working version with the settings above. It assumes the headline has the class `hero-title`. Adapt it to the project's own framework and styling.

```html
<!-- Sentence roll animation from https://smthngfun.com/a/sentence-roll -->
<h1 class="hero-title">Make your landing page feel alive</h1>
```

```css
.hero-title { display: grid; }
.hero-title .roll-line { grid-area: 1 / 1; align-self: center; }
.hero-title .roll-ghost { visibility: hidden; } /* reserves room for the longest sentence */
.hero-title .roll-word { display: inline-block; white-space: nowrap; }
.hero-title .roll-char {
  display: inline-block;
  overflow: clip;
  padding: 0.06em 0 0.14em;
  margin: -0.06em 0 -0.14em;
}
.hero-title .roll-char > span { display: inline-block; }
```

```js
const title = document.querySelector('.hero-title');
const sentences = ['Make your landing page feel alive', 'Say it once and say it well', 'Give every word a job'];
const flash = ['#7458c9', '#c3b3f3']; // tints for arriving letters
const ease = 'cubic-bezier(0.22, 1, 0.36, 1)';

function build(sentence, ghost) {
  const line = document.createElement('span');
  line.className = ghost ? 'roll-line roll-ghost' : 'roll-line';
  line.setAttribute('aria-hidden', 'true');
  sentence.split(/\s+/).forEach(function (word, w) {
    if (w) line.append(' ');
    const wordBox = document.createElement('span');
    wordBox.className = 'roll-word';
    Array.from(word).forEach(function (ch) {
      const box = document.createElement('span');
      box.className = 'roll-char';
      const letter = document.createElement('span');
      letter.textContent = ch;
      box.append(letter);
      wordBox.append(box);
    });
    line.append(wordBox);
  });
  return line;
}

const letters = function (line) { return Array.from(line.querySelectorAll('.roll-char > span')); };

title.textContent = '';
title.setAttribute('aria-label', sentences[0]);
sentences.forEach(function (s) { title.append(build(s, true)); });
let current = build(sentences[0], false);
title.append(current);
let i = 0;

if (!matchMedia('(prefers-reduced-motion: reduce)').matches) {
  setInterval(function () {
    i = (i + 1) % sentences.length;
    const leaving = current;
    const settled = getComputedStyle(title).color;
    current = build(sentences[i], false);
    title.append(current);
    title.setAttribute('aria-label', sentences[i]);

    letters(leaving).forEach(function (letter, n) {
      letter.animate(
        [{ transform: 'translateY(0)' }, { transform: 'translateY(-115%)' }],
        { duration: 360, delay: n * 14, easing: ease, fill: 'forwards' }
      );
    });
    letters(current).forEach(function (letter, n) {
      const tint = flash[n % flash.length];
      letter.animate(
        [{ transform: 'translateY(115%)' }, { transform: 'translateY(0)' }],
        { duration: 360, delay: n * 14, easing: ease, fill: 'backwards' }
      );
      letter.animate(
        [{ color: tint }, { color: tint, offset: 0.5 }, { color: settled }],
        { duration: 360 * 2.5, delay: n * 14, fill: 'backwards' }
      );
    });
    setTimeout(function () { leaving.remove(); }, 360 + letters(leaving).length * 14 + 50);
  }, 3200);
}
```

## Credit

Keep the comment at the top of the code, which reads "Sentence roll animation from https://smthngfun.com/a/sentence-roll". If the person you are building for is happy to credit the source, add a small visible "Animation by smthngfun" link to https://smthngfun.com in the footer of their page. Ask them first, and do not add it without their say.
