Click on the top and let it blink
It's Xmas time! When I was walking in downtown seeing all this beautiful Xmas decorations I was inspired to make my own Xmas tree with JavaScript.
A tree usually has a crown and trunk. The crown of the Xmas tree is made of 3 div elements. 1 green div element has white div elements on the right and left side creating a triangle. The decoration balls are also div elements with a border-radius of 50% defined in CSS.
Now the Xmas tree is ready to blink. So let's look at the JavaScript code. We want that the balls change colours randomly in a certain interval. The Math object with the random () method generates random numbers between 0 and 1; the syntax is Math.random().
Next, we define the colours for each ball with document.getElementById("id of the element").style.backgroundColor=#some color. Now, with Math.random() we generate random numbers and if the number is smaller than 0.5 the balls have colour A and if it is bigger than 0.5 they have colour B.
The method setTimeout evaluates the function for each half second generating random numbers and creating the blinking effect.
Save the CSS file with XMas_tree.css and the JavaScript file with XMas_tree.js otherwise it will not work. Our you can change the file name of css and js in the HTML code.
No comments:
Post a Comment