Copied SVG to clipboard
Something went wrong
Copied code to clipboard
Something went wrong

Default

User image

Default

Name

  • Osmo Discount
    -25%
The Vault/

Text Scramble (Load, Scroll, Hover)

Text Scramble (Load, Scroll, Hover)

Documentation

Webflow

Code

This resource uses a Club GSAP Script

Disclaimer: Club GSAP Script

Heads up! This script uses a paid GSAP plugin, so you’ll need a Club GreenSock membership to use it. Good news though—Osmo members get 25% off with our exclusive discount code!

Osmo Discount

-25%

Setup: External Scripts

External Scripts in Webflow

Make sure to always put the External Scripts before the Javascript step of the resource.

In this video you learn where to put these in your Webflow project? Or how to include a paid GSAP Club plugin in your project?

HTML

Copy
<script src="https://cdn.jsdelivr.net/npm/gsap@3.12.7/dist/gsap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gsap@3.12.7/dist/ScrollTrigger.min.js"></script>

<!-- ScrambleText and SplitText are paid plugins – you do NOT have permission to use the links below in your projects. -->
<!-- Please become a Club Member using our discount code above and host your own files --> 
<script src="https://cdn.jsdelivr.net/gh/ilja-van-eck/osmo/assets/gsap/ScrambleTextPlugin.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/ilja-van-eck/osmo/assets/gsap/SplitText.min.js"></script>

Step 1: Copy structure to Webflow

Copy structure to Webflow

In the video below we described how you can copy + paste the structure of this resource to your Webflow project.

Copy to Webflow

Webflow structure is not required for this resource.

Step 1: Add HTML

HTML

Copy
<div class="demo-group">
  <div class="scramble-section">
    <h1 data-scramble="load" class="scramble-heading">This heading will reveal with a basic scrambling effect<br>on page load</h1>
  </div>
  <div class="scramble-section u--bg-light">
    <h2 data-scramble="scroll" class="scramble-heading">this is an example of a heading that is triggered by a scrolltrigger</h2>
  </div>
  <div class="scramble-section">
    <h2 data-scramble-alt="" data-scramble="scroll" class="scramble-heading">You can even control the characters that are used during scramble</h2>
  </div>
  <div class="scramble-section u--bg-light">
    <h2 class="scramble-heading">and here&#x27;s how to work with scramble text on hover:</h2>
    <a data-scramble-hover="link" href="#" class="scramble-button w-inline-block">
      <p data-scramble-text="this text can be custom too" data-scramble-hover="target" class="scramble-button-text">How to scramble on hover</p>
    </a>
  </div>
</div>

HTML structure is not required for this resource.

Step 2: Add CSS

CSS

Copy
.scramble-section {
  grid-column-gap: 2em;
  grid-row-gap: 2em;
  flex-flow: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: 100vh;
  display: flex;
}

.scramble-section.u--bg-light {
  background-color: #efeeec;
}

.scramble-heading {
  text-align: center;
  letter-spacing: -.03em;
  text-transform: uppercase;
  max-width: 12em;
  margin: 0 auto;
  font-family: RM Mono, Arial, sans-serif;
  font-size: 3em;
  font-weight: 400;
  line-height: .9;
}

.scramble-button {
  color: #131313;
  text-transform: uppercase;
  border: 1px dotted #000;
  border-radius: .3125em;
  padding: .5em 1em;
  font-family: RM Mono, Arial, sans-serif;
  font-size: 1em;
  font-weight: 400;
  text-decoration: none;
}

.scramble-button-text {
  margin: 0;
}

Step 2: Add custom Javascript

Custom Javascript in Webflow

In this video, Ilja gives you some guidance about using JavaScript in Webflow:

Step 2: Add Javascript

Step 3: Add Javascript

Javascript

Copy
gsap.registerPlugin(ScrollTrigger, ScrambleTextPlugin, SplitText)

// Function to reveal stuff on load
function initScrambleOnLoad(){
  let targets = document.querySelectorAll('[data-scramble="load"]')
  
  targets.forEach((target) => {
  	// split into seperate words + letters 
    let split = new SplitText(target, {
      type: "words, chars",
      wordsClass: "word",
      charsClass: "char"
    });
    
    gsap.to(split.words, {
      duration: 1.2,
      stagger: 0.01,
      scrambleText: {
        text: "{original}",
        chars: 'upperCase', // experiment with different scramble characters here
        speed: 0.85,
      },
      // Once animation is done, revert the split to reduce DOM size
      onComplete: () => split.revert()
    })   
  })
}

// Function to reveal stuff on scroll
function initScrambleOnScroll(){
  let targets = document.querySelectorAll('[data-scramble="scroll"]')
  
  targets.forEach((target) => {
  	// Used this attribute to showcase a different character scramble, can be replaced with many scenarios
    let isAlternative = target.hasAttribute("data-scramble-alt")
    
    let split = new SplitText(target, {
      type: "words, chars",
      wordsClass: "word",
      charsClass: "char"
    });
    
    gsap.to(split.words, {
      duration: 1.4,
      stagger: 0.015,
      scrambleText: {
        text: "{original}", 
        chars: isAlternative ? '▯|' : 'upperCase',  // experiment with different scramble characters here
        speed: 0.95,
      },
      scrollTrigger: {
        trigger: target,
        start: "top bottom",
        once: true
      },
      // Once animation is done, revert the split to reduce DOM size
      onComplete: () => split.revert()
    })   
  }) 
}

function initScrambleOnHover(){
  let targets = document.querySelectorAll('[data-scramble-hover="link"]')
  
  targets.forEach((target) => {
    let textEl = target.querySelector('[data-scramble-hover="target"]')
    let originalText = textEl.textContent // save original text
    let customHoverText = textEl.getAttribute("data-scramble-text") // if this attribute is present, take a custom hover text
    
    let split = new SplitText(textEl, {
      type: "words, chars",
      wordsClass: "word",
      charsClass: "char"
    })
    
    target.addEventListener("mouseenter", () => {
      gsap.to(textEl, {
        duration: 1,
        scrambleText: {
          text: customHoverText ? customHoverText : originalText,
          chars: "◊▯∆|"
        }
      })
    })
    
    target.addEventListener("mouseleave", () => {
      gsap.to(textEl, {
        duration: 0.6,
        scrambleText: {
          text: originalText,
          speed: 2,
          chars: "◊▯∆"
        }
      })
    })    
  })
}

document.addEventListener("DOMContentLoaded", () => {
  initScrambleOnLoad()
  initScrambleOnScroll()
  initScrambleOnHover()
})

Step 3: Add custom CSS

Step 2: Add custom CSS

Custom CSS in Webflow

Curious about where to put custom CSS in Webflow? Ilja explains it in the below video:

CSS

Copy

Introduction

From my experience, first splitting the text into separate letters results in a nicer scramble effect than directly applying the scrambleText effect onto an element. This is a matter of preference though, and I definitely recommend playing around with it to find what you like best. To reduce the DOM size with all these separate characters, the script 'reverts' the splitting after the animation is done.

Application

The code above searches for any data-scramble="load" and data-scramble="scroll" elements on the page to reveal them, well, on load or scroll. As you'll see, adding the plugin is rather simple, and just goes in a regular GSAP tween. Definitely check out the GSAP Docs of the ScrambleTextPlugin to see all of the available options.

Resource Details

Code
Script
Scrolling
Scrolltrigger
Setup

Original source

Ilja van Eck

Creator Credits

We always strive to credit creators as accurately as possible. While similar concepts might appear online, we aim to provide proper and respectful attribution.