In this JavaScript Tutorial, you will learn how to do animation using setInternal API. Here, we will animate the HTML H1 tag’s color by setting the RGB Color mix in a regular interval of 10 Milli-seconds.
Code Snippet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>Java Script - Submit and Reset</title> <script type="text/javascript"> var Animate_Timer; var blue = 0; var red = 0; var green = 0; function Animate() { Animate_Timer = setInterval("AnimateH1()", 10); } function AnimateH1() { var H1_Element = document.getElementById("Heading1"); H1_Element.style.color=getRGB(); } function getRGB() { if (blue == 255) { red = red + 1; green = green + 1; if(red == 255 && green == 255) red = green = blue = 0; } else { blue = blue + 1; } return "rgb(" + red + "," + green + "," + blue + ")"; } function Stop() { clearInterval(Animate_Timer); } </script> </head> <body> <h1 id="Heading1"> Javascript Tutorial: Animation Using setInterval</h1> <Input type="button" value="Click to Animate" onclick="Animate()" /> <Input type="button" value="Stop" onclick="Stop()" /> </body> </html> |
Categories: JavaScript-Tube