This article demonstrates how to create a visually engaging text animation effect by individually animating characters within a sentence using CSS and JavaScript. The technique involves wrapping each character in a <span></span>
tag and applying CSS animations triggered by JavaScript.
Key Concepts:
<span></span>
, initiating the CSS animation.@keyframes
.aria-label
and aria-hidden
attributes to ensure screen readers can properly interpret the text.Implementation Steps:
Include jQuery: The code relies on jQuery for DOM manipulation.
HTML Structure: The HTML includes sentences with the class "sentence" and a button to trigger the animation.
<h1><span class="sentence title"></span>Fancy Slide In Text</h1> <h3><span class="sentence subtitle"></span>Embrace the fanciness!</h3> <div class="button">Click to Animate</div>
JavaScript (setUpCharacters): This function wraps each character of the sentences in <span>
tags.
JavaScript (triggerCharacters): This function adds the "active" class to each <span>
with a timed delay, creating the animation sequence.
CSS Animations: CSS @keyframes
define the animation itself (e.g., bounceUp
). The .active
class selector targets the spans for animation.
.sentence span { opacity: 0; position: relative; display: inline-block; } .sentence span.active { animation: bounceUp 600ms ease 0ms 1 normal both; } @keyframes bounceUp { /* ... animation definition ... */ }
Button Event Listener: The JavaScript binds the triggerCharacters
function to a button click.
Accessibility Considerations:
The article stresses the importance of accessibility. To make the animation accessible to screen readers, the original text is preserved using aria-label
on the parent element, while individual <span></span>
elements are hidden from screen readers using aria-hidden="true"
.
The article concludes with a CodePen link demonstrating the complete code and frequently asked questions addressing common concerns about character-level animation using CSS and JavaScript. The FAQ section provides further details on animation techniques, speed control, and accessibility best practices.
The above is the detailed content of Quick Tip: Single Character Transforms with CSS and JS. For more information, please follow other related articles on the PHP Chinese website!