Awaken the fireflies!
Use JavaScript Logic to change CSS, track mouse coordinates with Mouse Event Client X & Client Y Properties, and create dynamic visuals with CSS Keyframe Animations. Introduction of setInterval and removeChild.
To complete this project, students should have the following:
- Basic understanding of HTML structure and attributes.
- Basic understanding of CSS properties.
- Intermediate understanding of JavaScript and DOM.
To complete Part I, fulfill the following requirements:
- Set up your project file structure through the command line.
- Create the following:
- HTML, CSS, JS file
- Folder called assets
- Link all of your files correctly.
- Create a
div
with anid
ofcontainer
. - Inside of this
div
, create aheader tag
with anid
oftext
.
- Setup variables in your JS file to store the container div and the text header.
- Add an Event Listener to the
document
itself. This will listen for when the user moves their mouse.
- When the mouse is moved, we will track the mouse coordinates.
- The x and y coordinates of the mouse can be obtained as follows:
let x = event.clientX; let y = event.clientY;
- Change the innerHTML of the header text to display the x and y coordinates when the mouse is moved. We need to know which numbers are the x-coordinates and which numbers are the y-coordinates.
- We need to set an interval for how often we want our fireflies to appear.
- Create a variable
interval
that will store our interval method. The setInterval method takes in a function, then the milliseconds for how often we would like this function to run.
- This is an example of a setInterval method.
let interval = setInterval(myFunction, 3000);
- Create a setInterval method that takes in a function called
create
and runs it every 100 milliseconds.- Do not pass in
create()
with a parenthesis. The parenthesis will mean that we want our create function to run right away. Instead, we want to assign the create function to our setInterval method so that it is executed every so many milliseconds.
- Do not pass in
- We now need to create the function that will make our fireflies appear.
- Make a function called
create
. - In this function, declare a variable that will create a
div
element. This is an example of how to create an element with JavaScript.
let newParagraph = document.createElement('p');
- When this function runs, a new div will appear on the screen. Let's apply styles to these divs through JavaScript.
- This is an example of how to apply styles to an element in JavaScript.
newDiv.style.height = "20px";
- [element].style.[property] = "[value]";
- Style the div so that it has a
height
,width
,backgroundColor
,position
,left
, andtop
property. Setposition = 'absolute'
so that we can specify exact values in our left and top property. - Define a className on the newly created div using the syntax below. Name the class something meaningful and not just className.
newDiv.className += " className";
- Remember to include the space before className!
- Place this div on the document for us to see. To do so, we will need to append (attach), this div to our container div as so.
containerDiv.appendChild(newDiv);
- Now, let's go into our CSS file to make the animations happen!
- Style the div with class of container so that it has the full height and width of the entire screen.
- Create a
CSS Keyframe
animation that changes fromopacity = 1
to anotherbackground-color
,top = 0px
andopacity = 0
. - In your CSS, create a class selector for the className that you assigned to the newly created div in your
create
function.
- Set the following properties and values according to the name of your keyframe animation and how long you would like this to last:
animation-name
,animation-duration
, andopacity = 0
.
- View this in your browser to see the fireflies awaken!
- Randomize the height and width of the newly created Div so that every so many milliseconds, a different sized firefly appears.
- Change the
left
andtop
properties of the div so that the fireflies appear at the coordinates of your mouse! - Create an array that will take in each div as it appears. Create an if statement that will remove the first div in the array if the length of the array is greater than 10.