Thursday, December 8, 2022
HomeWeb DevelopmentThe best way to Create Animated Snow on a Web site (with...

The best way to Create Animated Snow on a Web site (with CSS and JavaScript)


On this tutorial, we’re going to implement an animated snow impact on a webpage utilizing CSS and vanilla JavaScript, simply in time for the vacation season.

‘Tis the season to be jolly so let it snow, let it snow, let it snow!

1. Markup with Vacation HTML

For our markup, we’ll have two major containers: a primary aspect containing the content material of our web page and a snow-container aspect that can show the snow.

2. Season’s Styling with CSS

On this demo, our primary content material has a gradient background color to provide it an evening sky impact. We obtain this utilizing the CSS linear-gradient perform. 

For simplicity, let’s say we’re constructing this in CodePen, so the markup goes within the HTML tab, the kinds within the CSS tab, and so forth.

Right here’s what our banner seems to be like now:

A blue gradient banner with text Happy HolidaysA blue gradient banner with text Happy HolidaysA blue gradient banner with text Happy Holidays

We’ll additionally use CSS to deal with the animation of every snow aspect. Every snowflake may have two animations: a fall animation to deal with the snowflake transferring from the highest to the underside of the display and a sway animation to deal with transferring the snowflake slowly aspect to aspect.

We’ll additionally model our snow container and the snow aspect we’ll be creating in JavaScript.

The animation property on this demo takes three values:

  • identify: That is the worth of the animation we created beneath keyframes.
  • timing: This determines how the animation progresses.
  • iteration: That is the variety of occasions the animation occurs. We use infinite to always repeat the animation of the snowflakes.

3. Fa la la la la Performance ♬

Now we are able to work on the enjoyable half: making it snow! First, within the JS tab in CodePen, let’s assign a worth to our container aspect:

For our snowflake content material, we’ll be utilizing HTML symbols (❄ ❅ ❆):

Snowflake characters using HTML codeSnowflake characters using HTML codeSnowflake characters using HTML code

We’ll create a snowContent array to include the image codes

With the intention to create a falling snow impact, we’ll must randomise the looks and animation of every snowflake.

We’ll use the Math.random() and Math.flooring() features to deal with producing the random values for our snow parts.

Now we are able to create a perform to generate a random model for every snowflake. We’ll generate a random place, dimension and animation length.

For our length and dimension fixed, we add a hard and fast quantity worth to make sure the random generated quantity has a minimal worth of the quantity being added (i.e. the bottom quantity for the animation length of any snowflake is 10s)

For our prime fixed, we assign a damaging worth so the snowflakes begins from any share above the seen display.

Subsequent, we’ll deal with appending the snowflakes to the snow container.

On this demo, we use a for loop to create a hard and fast variety of snowflakes and append them to the container. We additionally randomly assign the innerHTML as any of the values in our snowContent array.

Then, we’ll name the createSnow perform as soon as our web page masses utilizing the load occasion listener.

And it’s snowing! Click on Rerun to see the animated snow:

Eradicating Snowflakes

We are able to additionally select so as to add a perform to take away the snowflakes.

On this perform, we assign our snowContainer an opacity of 0 to permit it fade out easily after which, after half a second, we take away the container utilizing the .take away() methodology.

We are able to then select to name this perform nevertheless we would like. We are able to take away the snow when the consumer clicks wherever on the web page:

Or we are able to embrace it in setTimeout() perform so it disappears after a sure period of time:

Switching Out the Snowflakes

Since we’ve constructed this from scratch, we are able to additionally replace the aspect content material and animation to create different enjoyable results, like floating balloons:

and even animated Nyan Cat:

I hope you loved this animated snow tutorial, and it’s made you are feeling good and festive!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments