Contents
Creating Animations with CSS and JS on Your Site
Animations enhance user experience by guiding attention, providing feedback, and adding polish to interfaces. This article explores techniques for CSS-based and JavaScript-driven animations, performance considerations, and best practices to build responsive, accessible, and maintainable animations.
1. Why Use Animations
- User Guidance: Draw attention to changes or new content.
- Feedback: Indicate success, errors, or loading states.
- Branding: Reinforce your identity with subtle motion.
2. CSS Animations
2.1 Transitions
CSS transitions smoothly interpolate property changes.
button {
transition: background-color 0.3s ease, transform 0.2s ease-in-out
}
button:hover {
background-color: #0074D9
transform: translateY(-2px)
}
2.2 Keyframe Animations
Use @keyframes
to define complex sequences:
@keyframes slideIn {
0% { transform: translateX(-100%) opacity: 0 }
50% { opacity: 1 }
100% { transform: translateX(0) }
}
.modal {
animation: slideIn 0.5s ease-out forwards
}
2.3 Animation Properties
Property | Purpose |
---|---|
animation-name | References @keyframes |
animation-duration | Defines total time |
animation-timing-function | Easing curve |
animation-delay | Start offset |
animation-iteration-count | Number of repeats |
animation-fill-mode | State before/after |
3. JavaScript Animations
JavaScript offers control over timing, dynamic properties, and interactive sequences.
3.1 requestAnimationFrame
requestAnimationFrame schedules callbacks synced to the browser’s repaint cycle (60fps max).
function animateBox(timestamp) {
const box = document.querySelector(.box)
const progress = Math.min((timestamp - start) / 1000, 1)
box.style.transform = translateX( (progress 200) px)
if (progress < 1) requestAnimationFrame(animateBox)
}
let start = performance.now()
requestAnimationFrame(animateBox)
3.2 Web Animations API
The Web Animations API provides a unified timing model:
elem.animate([
{ transform: scale(0) },
{ transform: scale(1) }
], {
duration: 400,
easing: ease-out
})
4. Performance Considerations
- Use Transform amp Opacity: Avoid layout-triggering properties.
- Layer Promotion:
will-change
judiciously to create composite layers. - Avoid Long Scripts: Break complex tasks into smaller chunks.
For in-depth guidance, refer to Google Web Fundamentals.
5. Accessibility amp Usability
- Reduced Motion: Honor
prefers-reduced-motion
. Provide an option to disable non-essential animations. - Focus Indicators: Ensure keyboard users can track focus without losing context.
@media (prefers-reduced-motion: reduce) {
{
animation-duration: 0.01ms !important
transition-duration: 0.01ms !important
}
}
6. Debugging Techniques
- Use browser devtools’ Animation Inspector to pause, slow down, and inspect keyframes.
- Log frame times in JS:
console.time
/console.timeEnd
for performance.
7. Libraries amp Frameworks
- GSAP – high-performance, robust feature set.
- Anime.js – lightweight and declarative.
- Framer Motion – React-focused animations.
8. Advanced Techniques
- Choreography: Sequence multiple elements via
animation-delay
or JS promises. - Physics-based: Spring and decay effects using libraries or custom JS.
- SVG amp Canvas: Animate shapes with
transitions orCanvas
frames.
9. Maintainability amp Workflow
- Centralize keyframes in a single CSS file.
- Use design tokens for durations, easings, and delays.
- Document each animation’s intent in comments.
Conclusion
Combining CSS animations for simple, declarative effects with JavaScript for dynamic control offers a powerful toolkit. Prioritize performance, accessibility, and clarity in your code. Experiment, measure, and iterate to create engaging user experiences that feel both polished and purposeful.
|
Acepto donaciones de BAT's mediante el navegador Brave 🙂 |