Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Creating Interactive CSS3 Background Animations with JavaScript

Tech 1
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS3 Animation Example</title>
    <style>
        .initial-state {
            background-color: #ff0000;
            padding: 2rem;
            color: white;
            font-size: 1.3rem;
        }

        @keyframes gradientCycle {
            0% { background-color: #ff0000; }
            25% { background-color: #ffff00; }
            50% { background-color: #0000ff; }
            100% { background-color: #00ff00; }
        }

        @-moz-keyframes gradientCycle {
            0% { background-color: #ff0000; }
            25% { background-color: #ffff00; }
            50% { background-color: #0000ff; }
            100% { background-color: #00ff00; }
        }

        @-webkit-keyframes gradientCycle {
            0% { background-color: #ff0000; }
            25% { background-color: #ffff00; }
            50% { background-color: #0000ff; }
            100% { background-color: #00ff00; }
        }

        @-o-keyframes gradientCycle {
            0% { background-color: #ff0000; }
            25% { background-color: #ffff00; }
            50% { background-color: #0000ff; }
            100% { background-color: #00ff00; }
        }

        .animated-state {
            animation: gradientCycle 5s;
            -moz-animation: gradientCycle 5s;
            -webkit-animation: gradientCycle 5s;
            -o-animation: gradientCycle 5s;
            padding: 2rem;
            color: white;
            font-size: 1.3rem;
        }
    </style>
</head>
<body>
    <div class="initial-state" id="demoDiv">Hello, CSS3 Animations!</div>
    <button type="button" id="animateBtn">Start Background Animation</button>

    <script>
        const animateButton = document.getElementById('animateBtn');
        const demoElement = document.getElementById('demoDiv');

        animateButton.addEventListener('click', () => {
            console.log('Button clicked to start animation');
            if (demoElement.classList.contains('initial-state')) {
                demoElement.classList.remove('initial-state');
            }
            demoElement.classList.add('animated-state');
        });
    </script>
</body>
</html>
  1. Define Animation Keyframes: Use @keyframes to specify a sequnece of style changes over time. Keyframe percentages (0% = start, 100% = end) map to specific property values. Vendor prefixes ensure compatibility with older browsers like Firefox, Safari/Chrome, and Opera.

  2. Encapsulate Animation in CSS Class: Create a dedicated class that references the defined keyframe animation via the animation property. Include vendor-prefixed versions of this property to maintain compatibility.

  3. Apply Animation Dynamically: Use JavaScript to listen for user interactions (e.g., button clicks) and manipulate the DOM element’s classList to apply the animated state class, replacing the initial static style.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.