It works by extracting text content and wrapping individual units (letters, words, or lines) in styled spans, then applying CSS animations with calculated delays to create fluid, sequential effects.
1. Install AnimText and import it into your main JavaScript file.
npm i animtext npm i @hellouxpavel/cssanimation
import animtext from "animtext"; import "@hellouxpavel/cssanimation";
2. For direct use in an HTML file, add the {css}animation library to your <head> and the AnimTex.js before your closing </body> tag.
<link rel="stylesheet" href="/path/to/cssanimation.min.css" /> <script src="/path/to/animtext.min.js"></script>
3. To animate an element, you need two things: the cssanimation class and a data-at-* attribute.
class="cssanimation": This class is required. It’s the hook that AnimText uses to find elements to process.data-at-sequence="ca__fx-fadeIn": This attribute tells AnimText to animate the element’s text letter-by-letter (sequence) using the ca__fx-fadeIn animation from the {css}animation library.<h1 class="cssanimation" data-at-sequence="ca__fx-fadeIn">CSSScript</h1>
4. AnimText supports multiple animation patterns through specific data attributes:
data-at-sequence: Animates letters from first to last in orderdata-at-random: Randomizes letter animation sequencedata-at-reverse: Animates letters from last to firstdata-at-word: Animates complete words sequentiallydata-at-line: Animates text lines based on line breaks or custom separators5. The data-at-delay attribute accepts single or multiple delay values in milliseconds:
<p class="cssanimation" data-at-sequence="ca__fx-fadeIn" data-at-delay="50 100 200"> Varied timing rhythm </p>
6. Set consistent animation duration across all units:
<h3 class="cssanimation" data-at-word="ca__fx-slideInUp" data-at-base-duration="800"> All words animate for 800ms </h3>
7. Control how lines are split using the separator attribute:
<p class="cssanimation" data-at-line="ca__fx-fadeIn" data-at-separator="dot"> First sentence. Second sentence. Third sentence. </p>
8. Assign different animations to each unit by space-separating class names:
<h2 class="cssanimation" data-at-word="ca__fx-fadeIn ca__fx-moveFromTop ca__fx-bounceInLeft"> Each word gets different animation </h2>
9. For content loaded after initial page render, manually trigger re-initialization:
animtext.init();
This scans the DOM for new elements with the cssanimation class and applies appropriate animations.
Q: Why are my animations not working?
A: The most common reason is forgetting to include the cssanimation library’s stylesheet. AnimText only creates the <span> structure and applies timing; it relies on an external library like cssanimation for the actual keyframe definitions. Also, confirm the target element has the cssanimation class.
Q: Can I use my own CSS animation classes instead of the ones from cssanimation?
A: Yes. AnimText is class-agnostic. You can pass any CSS class name to the data-at-* attributes as long as that class and its @keyframes are defined in your project’s CSS.
Q: Can I mix data-at-word and data-at-line on the same element?
A: No. Only one data-at-* type is processed per element. Chain them on different elements if needed.
The post Apply Smooth Animations to Text Using Data Attributes – AnimText.js appeared first on CSS Script.
AvalynxAutocomplete is a lightweight Bootstrap autocomplete component that converts text inputs into searchable, keyboard-navigable dropdown…
LANSING, MI (WOWO) Michigan officials are calling for new legislation and increased security funding following…
LANSING, MI (WOWO) Michigan officials are calling for new legislation and increased security funding following…
LANSING, MI (WOWO) Michigan officials are calling for new legislation and increased security funding following…
LANSING, MI (WOWO) Michigan officials are calling for new legislation and increased security funding following…
MACOMB COUNTY, MI. (WOWO) A 40-year-old man convicted in a petition signature fraud scheme tied…
This website uses cookies.