How to Use in Your HTML (Practical Guide)
Using custom attributes like data-sd-animate can add lightweight, semantic hooks for JavaScript and CSS animations without relying on classes or inline scripts. This guide shows practical ways to use to trigger and control animations for inline text and small UI elements.
What it is
- Attribute: data-sd-animate — a data attribute placed on HTML elements (commonly ) to signal an animation.
- Purpose: Provide a clear, semantic marker that JavaScript or CSS can target to apply animations (e.g., fade, slide, typewriter).
Basic HTML examples
- Simple inline usage:
html
<p>Welcome to our site — <span data-sd-animate=“fade”>enjoy your stay</span>!</p> - Multiple animations:
html
<p><span data-sd-animate=“type”>Hello</span> <span data-sd-animate=“blink”>|</span></p>
CSS-only animations
- Fade-in using attribute selector:
css
[data-sd-animate=“fade”] { opacity: 0; transition: opacity 600ms ease;}[data-sd-animate=“fade”].is-visible { opacity: 1;}Add the .is-visible class with JS when the element should appear.
JavaScript examples
- Simple reveal on load:
js
document.querySelectorAll(’[data-sd-animate=“fade”]’).forEach(el => { requestAnimationFrame(() => el.classList.add(‘is-visible’));}); - Typewriter effect:
js
function type(el, speed = 50) { const text = el.textContent; el.textContent = “; let i = 0; const timer = setInterval(() => { el.textContent += text.charAt(i++); if (i === text.length) clearInterval(timer); }, speed);}document.querySelectorAll(’[data-sd-animate=“type”]’).forEach(el => type(el));
Accessibility tips
- Respect prefers-reduced-motion:
css
@media (prefers-reduced-motion: reduce) { [data-sd-animate] { transition: none; animation: none; }} - Ensure animations don’t obscure content or interfere with screen readers; keep ARIA attributes when needed.
Practical patterns
- Use semantic values like “fade”, “slide-up”, “type”.
- Combine with IntersectionObserver to animate on scroll.
- Keep animations short (200–800ms) and optional for accessibility.
Conclusion
Using lets you declaratively mark elements for animation while keeping HTML semantic. Pair it with CSS and small JS utilities for performant, accessible UI enhancements.
Leave a Reply