gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
Create a simple button toggles the animation
- User
Create a simple button toggles the animation
- Assistant
To create a simple button that toggles animation, you can use HTML, CSS, and JavaScript. Here's an example where we animate a square changing colors, and you can toggle the animation on and off by clicking a button:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Toggle Animation</title> <style> #animateBox { width: 100px; height: 100px; background-color: red; position: relative; transition: background-color 1s; } @keyframes colorChange { 0% { background-color: red; } 50% { background-color: blue; } 100% { background-color: red; } } .animated { animation: colorChange 2s infinite; } </style> </head> <body> <div id="animateBox"></div> <button id="toggleButton">Toggle Animation</button> <script> const animateBox = document.getElementById('animateBox'); const toggleButton = document.getElementById('toggleButton'); toggleButton.addEventListener('click', () => { animateBox.classList.toggle('animated'); }); </script> </body> </html>