A JavaScript library to shuffle the text content of a DOM element with an animated effect.

Overview

build npm version Coverage Status License: MIT

shuffle-letters.js

A JavaScript library to shuffle the text content of a DOM element with an animated effect.

NOTE: This library is a port to vanilla JavaScript of the Shuffle Letters Effect jQuery plugin.

Demo

Check here for a live demo.

Install

$ npm install shuffle-letters --save

Usage

The library is exported in UMD, CommonJS, and ESM formats. You can import it the following ways:

Using ESM import statement

import shuffleLetters from 'shuffle-letters';

Using CommonJS require statement

const shuffleLetters = require('shuffle-letters');

// If you use a bundler like Webpack, you may need to import it the following way 
// as it might try to use the ESM module instead of the CommonJS.
const shuffleLetters = require('shuffle-letters').default;

Old school browser global

<script src="https://unpkg.com/shuffle-letters"></script>

API

shuffleLetters(element, [config])

Params

Param Type Default Description
element HTMLElement The HTML element whose textContent we want to apply the shuffle effect.
[config] object {} Configurable object to override the default options.
[config.text] string "''" Alternative text that can be used instead of the textContent of the element.
[config.iterations] number 8 The number of times the characters will be shuffled before the animation ends.
[config.fps] number 30 The amount of frames per second that the animation runs.
[config.onComplete] function () => void A callback function that is called when the animation of the effect is complete.

Returns: function - Returns a function that when called, it clears the timeoutID which identifies the timer created by the call to setTimeout() that is used internally for the shuffle effect. Useful for cleanup purposes, for example when you want to remove the element before the animation is completed.

Usage examples

Example 1 - Default options

HTML

<h1>This is a funcy title</h1>

JavaScript

  shuffleLetters(document.querySelector('h1'));

Example 2 - Custom options

HTML

<h1>This is a funcy title</h1>

JavaScript

shuffleLetters(document.querySelector('h1'), {
  text: 'Yet another funcy title',
  iterations: 12,
  fps: 60,
  onComplete: el => {
    console.log('Effect was completed.');
    console.log(el);
  }
});

Example 3 - Shuffle many elements

HTML

<ul>
  <li>List item 1</li>
  <li>List item 2</li>
  <li>List item 3</li>
  <li>List item 4</li>
</ul>

JavaScript

const list = document.querySelector('ul');
const listItems = list.querySelectorAll('li');

Array.prototype.forEach.call(listItems, (element, index) => {
  shuffleLetters(element, {
    onComplete: elm => {
      if (index === listItems.length - 1) {
        console.log('All effects were completed');
      }
    }
  });
});

Example 4 - Cleanup

HTML

<h1>This is a funcy title</h1>

JavaScript

  const clearShuffleLetters = shuffleLetters(document.querySelector('h1'), {
    iterations: 200
  });
  
  // Cleanup after 1 second
  setTimeout(() => clearShuffleLetters(), 1000);

License

The MIT License (MIT)

You might also like...

I forgot about el.outerHTML so I made this, it takes a DOM element and returns its html as string

I forgot about el.outerHTML so I made this, it takes a DOM element and returns its html as string

htmlToString Convert html/DOM element to string Works with rendered and virtual DOM Installation npm install htmltostring Or using CDN script src="ht

Jul 22, 2022

Creates a table of contents in a DOM element optionally linked to with anchors. No jQuery or other dependencies.

HTML-Contents Creates a table of contents in a DOM element optionally linked to with anchors. No dependencies. @psalmody Get It We're on npm: npm i ht

Oct 25, 2022

Inspiration for revealing content by morphing the action element.

Morphing Buttons Concept Inspiration for revealing content by morphing the action element. Article on Codrops Demo Integrate or build upon it for free

Aug 18, 2022

Little Alpine.js plugin to add a typewriter effect to any HTML element.

Little Alpine.js plugin to add a typewriter effect to any HTML element.

⌨️ Alpine Typewriter ⌨️ An Alpine.js plugin to add a typewriter effect to any HTML element. 🚀 Installation CDN Include the following script tag in

Dec 28, 2022

A lightweight JavaScript library that renders text in a brilliant style by displaying strings of random characters before the actual text.

A lightweight JavaScript library that renders text in a brilliant style by displaying strings of random characters before the actual text.

cryptoWriter.js A lightweight javascript library which creates brilliant text animation by rendering strings of random characters before the actual te

Sep 13, 2022

Format input text content when you are typing...

Cleave.js Cleave.js has a simple purpose: to help you format input text content automatically. Features Credit card number formatting Phone number for

Dec 29, 2022

A simple and useful jquery plugin that allows you to create a Text Area Character Count Effect with limited warning.

A simple and useful jquery plugin that allows you to create a Text Area Character Count Effect with limited warning.

jquery-character-counter A simple and useful jquery plugin that allows you to create a Text Area Character Count Effect with limited warning. #Demo Us

Dec 28, 2020

jQuery plugin for adding some effect to text

jQuery plugin for adding some effect to text

jquery.textroll.js DEMO and examples jQuery plugin for adding some effect to text : DEMO Usage Simple $(".someClass").textroll(); With Options

Jan 15, 2022

A small javascript DOM manipulation library based on Jquery's syntax. Acts as a small utility library with the most common functions.

Quantdom JS Quantdom is a very small (about 600 bytes when ran through terser & gzipped) dom danipulation library that uuses a Jquery like syntax and

Aug 16, 2022
Comments
Releases(v1.0.1)
A little animation for a big menu where the letters of a word shuffle to become the first letter of each menu item.

Letter Shuffle Animation for a Menu A little animation for a big menu where the letters of a word shuffle to become the first letter of each menu item

Codrops 29 Dec 4, 2022
Shuffle texts.

shuffleText This is JavaScript file. When mouse over element shuffle texts in html. It is easy to use it for everyone. Update Infomation If you make s

Toshiya Marukubo 9 Apr 5, 2022
A javascript library to generate animated wavy text!

Wavy Text Animation Library A JavaScript Library which allows you to animate infinite words in an infinite loop in a modern wavy way! Options Vanilla

Murtuzaali Surti 12 Oct 9, 2022
JavaScript micro-library: pass in an element and a callback and this will trigger when you click anywhere other than the element

Add a click listener to fire a callback for everywhere on the window except your chosen element. Installation run npm install @lukeboyle/when-clicked-

Boyleing Point 5 May 13, 2021
A little JavaScript plugin to generate PDF, XLS, CSV and DOC from JavaScript Object or DOM element only from the frontend!

?? JavaScript Object to csv, xls, pdf, doc and DOM to html generator ?? A little JavaScript plugin to generate PDF, XLS, CSV and DOC from JavaScript O

null 61 Jan 7, 2023
An extension of DOM-testing-library to provide hooks into the shadow dom

Why? Currently, DOM-testing-library does not support checking shadow roots for elements. This can be troublesome when you're looking for something wit

Konnor Rogers 28 Dec 13, 2022
Flexible, powerful and modern library for creating the running horizontal blocks effect, also known as ticker or the «marquee effect».

Cuberto Reeller Flexible, powerful and modern library for creating the running horizontal blocks effect, also known as ticker or the «marquee effect».

Cuberto 41 Jan 4, 2023
Custom Vitest matchers to test the state of the DOM, forked from jest-dom.

vitest-dom Custom Vitest matchers to test the state of the DOM This library is a fork of @testing-library/jest-dom. It shares that library's implement

Chance Strickland 14 Dec 16, 2022
A custom element for rendering stylable (light DOM) Markdown

Motivation There are many web components these days to render Markdown to HTML. Here are a few: <zero-md> <marked-element> …and I’m sure many others H

Lea Verou 252 Dec 20, 2022
Create DOM element and bind observables on it.

rx-domh Create DOM element and bind observables on it. Inspired by Binding.scala and react-flyd, I made this. Just a simple todo example: /** @jsx h *

xialvjun 4 Feb 6, 2018