I can help, but your title looks cut off: “Use data-sd-animate=”. I’ll assume you want an article about using the HTML attribute data-sd-animate (e.g., “Use data-sd-animate=”…“> for animated spans”). I’ll write a concise article explaining what it is, how to use it, examples, accessibility considerations, and troubleshooting. If you meant a different title, tell me and I’ll revise.
Use for simple HTML animations
What it is
The attribute data-sd-animate is a custom data- attribute (an HTML5 data attribute) used to mark elements for animation by JavaScript or CSS. Browsers ignore unknown data- attributes by default, so scripts and styles can read them to trigger behavior without affecting semantics.
When to use it
- You want declarative, attribute-driven animations on inline elements (like spans) without adding classes.
- You have a small JS animation library or script that reads data attributes to configure timing, type, or delay.
- You need per-element animation parameters embedded in markup for content editors or dynamic content.
Basic usage
Place the attribute on an element and give it a value describing the animation type or parameters:
Hello
Common patterns for values:
- Simple token: “fade-in”, “slide-up”, “shake”.
- JSON-like options: ’{“type”:“fade”,“duration”:“500ms”,“delay”:“200ms”}’ (parse with JSON.parse).
- Space-separated tokens: “fade-in slow” — parse in JS.
Example: CSS-driven (class toggle)
HTML:
Welcome
CSS:
.fade-in {
opacity: 0;
transform: translateY(8px);
transition: opacity 400ms ease, transform 400ms ease;
}
.fade-in.animate {
opacity: 1;
transform: translateY(0);
}
JavaScript (simple observer):
const els = document.querySelectorAll(’[data-sd-animate]’);const obs = new IntersectionObserver((entries) => {entries.forEach(e => { if (e.isIntersecting) e.target.classList.add(‘animate’); });});els.forEach(el => obs.observe(el));
Example: Parsing options from the attribute
HTML:
Item
JS:
const el = document.querySelector(’[data-sd-animate]’);const opts = JSON.parse(el.getAttribute(‘data-sd-animate’));el.style.transitionDuration = opts.duration + ‘ms’;setTimeout(() => el.classList.add(‘animate-’ + opts.type), opts.delay);
Accessibility considerations
- Prefer reduced-motion users: respect the prefers-reduced-motion media query and avoid forced motion.
- Ensure animations don’t convey essential information; if they do, provide non-animated alternatives.
- Avoid animations that trigger seizures (rapid flashing).
Performance tips
- Animate transform and opacity (GPU-friendly), avoid animating layout properties (width/height/left/top).
- Batch DOM reads/writes, use will-change sparingly.
- Use IntersectionObserver for on-scroll animations to avoid scroll handlers.
Troubleshooting
- Attribute value not recognized: ensure your script queries the correct attribute name and parses its format.
- Animation not running: check that the class toggles and CSS selectors match; verify computed styles and transition properties.
- JSON parsing error: validate quotes and escape characters in HTML; consider URL-encoding or using data attributes for individual fields (data-sd-type, data-sd-duration).
If you want, I can:
- Produce a ready-to-drop script and CSS for multiple animation types.
- Convert the examples into a small library that supports defaults and prefers-reduced-motion.
Which would you prefer?
Leave a Reply