Infosec Matrix

Collection of Best Writeups for HackTheBox, Portswigger, Bug Bounty, TryHackme, OverTheWire…

Building Stunning Animations with CSS and JavaScript

Madhu deepak
Infosec Matrix
Published in
3 min readJan 4, 2025

--

image by sharathchandark

Animations are no longer just a “nice-to-have” feature for websites; they’ve become a critical aspect of creating engaging user experiences. When done right, animations can breathe life into your design, guide user interactions, and set your website apart. Two powerful tools for achieving this are CSS and JavaScript. This article will walk you through creating stunning animations using both, and explore best practices for performance and user engagement.

Why Use Animations in Web Design?

Animations are more than eye candy. They serve functional purposes, including:

  • Enhancing User Experience: Smooth transitions and micro-interactions make websites feel more intuitive.
  • Guiding Users: Animations can direct users’ attention to important elements, like call-to-action buttons.
  • Brand Building: Creative animations aligned with your brand identity help set the tone and distinguish your site.

Getting Started with CSS Animations

CSS animations are lightweight, easy to implement, and perfect for basic effects like fades, slides, and rotations. Here’s a step-by-step guide:

1. Keyframes: The Building Block of CSS Animations

Keyframes define the stages of an animation. For example, let’s create a simple bounce effect:

@keyframes bounce {
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-20px);
}
}

2. Applying the Animation

Use the animation property to link the keyframes to an element:

.button {
animation: bounce 1s infinite;
}

This code creates an bouncing continuous impact for any element with the class button.

3. Timing Functions for Smoother Transitions

The ease, ease-in, and ease-out timing functions control the pacing of animations:

.button {
animation: bounce 1s ease-in-out infinite;
}

Enhancing Animations with JavaScript

While CSS is ideal for simple effects, JavaScript takes animations to the next level by providing:

  • Interactivity: Trigger animations based on user actions like clicks, scrolls, or hovers.
  • Complex Sequences: Manage multiple animations in a specific order.
  • Dynamic Values: Create animations that respond to real-time data or user input.

1. The Basics: Animating with Element.animate()

The animate() method is part of the Web Animations API, allowing you to define animations directly in JavaScript:

const element = document.querySelector('.box');

element.animate([
{ transform: 'translateY(0)' },
{ transform: 'translateY(-20px)' },
{ transform: 'translateY(0)' }
], {
duration: 1000,
iterations: Infinity
});

This snippet creates a bouncing box similar to our CSS example but controlled via JavaScript.

2. Event-Driven Animations

Combine JavaScript with event listeners for interactive effects:

const button = document.querySelector('.button');

button.addEventListener('click', () => {
button.animate([
{ transform: 'scale(1)' },
{ transform: 'scale(1.2)' },
{ transform: 'scale(1)' }
], {
duration: 500
});
});

Here, clicking the button triggers a scaling animation.

Combining CSS and JavaScript

In many cases, combining CSS and JavaScript yields the best results. For example, use CSS for defining animations and JavaScript for controlling when they occur:


const element = document.querySelector('.box');

element.classList.add('animate');

.box.animate {
animation: fadeIn 1s forwards;
}

@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}

Best Practices for Animations

To ensure your animations are effective and user-friendly, keep these tips in mind:

  1. Optimize Performance:
  • Use transform and opacity properties for animations. These are GPU-accelerated and minimize reflows.
  • Avoid animating properties like width or height that require recalculating layouts.

2. Keep It Subtle:

  • Overusing animations can distract users. Stick to meaningful effects that enhance usability.

3. Test Across Devices:

  • Ensure your animations work seamlessly on various screen sizes and browsers.

4. Accessibility Matters:

  • Respect users’ motion preferences. Use media queries like @media (prefers-reduced-motion) to disable animations for users who prefer minimal motion.
@media (prefers-reduced-motion: reduce) {
.box {
animation: none;
}
}

Tools and Libraries for Advanced Animations

If you’re looking to save time or create complex animations, consider these popular libraries:

  • GSAP (GreenSock): A powerful JavaScript library for creating high-performance animations.
  • Anime.js: Ideal for lightweight, highly customizable animations.
  • Lottie: Enables animation rendering from JSON files exported from Adobe After Effects.

Wrapping Up

CSS and JavaScript animations are indispensable tools for modern web design. By mastering these techniques, you can create immersive, user-friendly experiences that leave a lasting impression. Remember, the key to effective animations is balance — use them to enhance your design, not overwhelm it.

--

--

Infosec Matrix
Infosec Matrix

Published in Infosec Matrix

Collection of Best Writeups for HackTheBox, Portswigger, Bug Bounty, TryHackme, OverTheWire, PwnCollege, PicoCTF, and More.

Madhu deepak
Madhu deepak

Written by Madhu deepak

Software Engineer and Developer