- var phrases = [
- { text: 'Hello world' },
- { text: 'Good morning', duration: 0.4, stagger: 0.1 },
- { text: 'Great days are coming', duration: 0.2, stagger: -0.05 },
- { text: 'Live for tomorrow', duration: 0.1, stagger: 0.1 },
- { text: 'Roll with the punches', stagger: -0.015},
- { text: 'Have a blast', duration: 1, stagger: 0.025 },
- { text: 'Bet on black', duration: 0.8, stagger: -0.025 },
- { text: 'Tuck and roll', duration: 0.1, stagger: -0.075 }
- ]

ul.grid
  each phrase in phrases
    li.grid__item
      h2.letter-wrap(data-duration=phrase.duration data-stagger=phrase.stagger)= phrase.text
View Compiled
@import url('https://fonts.googleapis.com/css?family=Source+Sans+Pro:700');

$bp-sm: 600px;
$bg-color: seagreen;

:root {
  --bg-color: #{$bg-color};
  --primary-clr: black;
  --secondary-clr: white;
  --base-ff: 'Source Sans Pro', sans-serif;
  --h2-fs: 2.25rem;
  --pad: 2rem;
}

* {
  box-sizing: border-box;
}

.grid {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  min-height: 100vh;
  font-family: var(--base-ff);
  
  &__item {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 100%;
    width: 50vw;
    height: 50vh;
    padding: var(--pad);
    background-color: var(--bg-color);
    
    @media (min-width: $bp-sm) {
      flex: 1 0 50%;
    }

    &:nth-child(even) {
      background-color: lighten($bg-color, 6%);
    }
    &:nth-child(3n) {
      background-color: darken($bg-color, 2%);
    }
    &:nth-child(4n) {
      background-color: lighten($bg-color, 8%);
    }
    &:nth-child(5n) {
      background-color: darken($bg-color, 6%);
    }
  }
}

h2 {
  font-size: var(--h2-fs);
  text-align: center;
  color: var(--primary-clr);
}

.letter-wrap {
  display: inline-block;
  position: relative;
}

.letter-wrap * {
  pointer-events: none;
}

.letter-wrap__word {
  display: inline-flex;
  position: relative;
  overflow: hidden;
}

.letter-wrap__char {
  display: inline-block;
  position: relative;
  text-transform: uppercase;
  
  &-inner {
    display: inline-block;
    
    &:after {
      content: attr(data-letter);
      position: absolute;
      top: 100%;
      left: 0;
      color: var(--secondary-clr);
    }
  }
}
View Compiled
const letterWrapClass = 'letter-wrap';
const letterWrapElements = document.getElementsByClassName(letterWrapClass);

[...letterWrapElements].forEach(el => {
  letterWrap(el, letterWrapClass);
  letterAnimation(el, letterWrapClass);
});



function letterWrap(el, cls) {
  const words = el.textContent.split(' ');
  const letters = [];
  
  cls = cls || 'letter-wrap'
  
  words.forEach(word => {
    let html = '';
    for (var letter in word) {
      html += `
        <span class="${cls}__char">
          <span class="${cls}__char-inner" data-letter="${word[letter]}">
            ${word[letter]}
          </span>
        </span>
      `;
    };
    
    let wrappedWords = `<span class="${cls}__word">${html}</span>`;
    letters.push(wrappedWords);
  });
  
  return el.innerHTML = letters.join(' ');
}

function letterAnimation(el, cls) {
  const tl = new TimelineMax({ paused: true });
  const characters = el.querySelectorAll(`.${cls}__char-inner`);
  const duration = el.hasAttribute('data-duration') ? el.dataset.duration : 0.3;
  const stagger = el.hasAttribute('data-stagger') ? el.dataset.stagger : 0.03;
  
  el.animation = tl.staggerTo(characters, duration, {
    y: '-100%',
    ease: Power4.easeOut
  }, stagger);
      
  el.addEventListener('mouseenter', (event) => event.currentTarget.animation.play());
  el.addEventListener('mouseout', (event) => el.animation.reverse());
}
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/gsap/2.0.1/TweenMax.min.js