It snows javascript to realize snowflakes flying

  • 2021-02-17 06:15:55
  • OfStack

The example of this paper for everyone to share the snowflake flying effect javascript implementation, for your reference, the specific content is as follows


1, js dynamically create DIV, specify CLASS class set different background image style to display different snowflake effect.

2, js Gets the created DIV and changes the value of its top attribute. When the fall height is greater than the screen height, delete the moved div

3. Do not spray if it is not perfect

Effect preview: http: / / wjf444128852 github. io demo02 / snow/index html

HTML code:

<!DOCTYPE html>
<html lang="en">
 <meta charset="UTF-8">
 <title> snow </title>
 <link rel="stylesheet" href="css/index.css">
 <script src="js/move.js"></script>
 <div class="snow_parent" id="js_sonw">

CSS code

 list-style: none;
 border: none;
 width: 100%;
 position: relative;
 width: 100%;
 overflow: hidden;
 margin: 0 auto;
.snow_parent div.parent{
 background-image: url(../img/snow.png);
 float: left;
 -webkit-transform: scale(.1);
 -moz-transform: scale(.1);
 -o-transform: scale(.1);
 -ms-transform: scale(.1);
 transform: scale(.1);
 position: absolute;
 width: 180px;
 height: 180px;
 background-position:0 0;
 background-repeat: no-repeat;
 top: -95px;
 width: 140px;
 height: 140px;
 background-position:-220px -18px;
 top: -75px;
 height: 150px;
 background-position:-400px -15px;
 top: -80px;
 width: 160px;
 height: 160px;
 background-position:-10px -206px; 
 top: -85px;

JS code:

 creatBy jiucheng 2016-4-24
//  create DIV
function creatDiv(){
 //  create DIV And appends to the parent element 
 var snowDiv=document.createElement("div");
 //  To create a DIV the class Is random and displays different snowflakes 
 var whatName=["snow_one parent","snow_two parent","snow_three parent","snow_four parent"];
 var index=Math.floor(Math.random()*whatName.length);
 //  To get the DIV the left Attribute values ( random ) And assign a value to the created DIV
 var whatLeft=getLeft()+'px';;
 return snowDiv;
//  Access to random left Attribute values 
function getLeft(){
 //  To get the DIV One of the biggest left The value of the attribute is the width of the parent element 
 var eleParent=document.getElementById("js_sonw");
 //  Gets all of the parent elements style style 
 var style=window.getComputedStyle(eleParent);
 // CSS In the left It's a negative number and we have to subtract it from here 
 var maxWidth=parseInt(style.width)+70;
 //  Let the creating DIV the left For the random value 
 var randomLeft=Math.floor(Math.random()*maxWidth);
 return randomLeft;
//  Let's move it down 
function moveDown(){
 //  Getting Moving Objects 
 var moveElem=creatDiv();
 //  Gets all of the moved object style Attribute values 
 var eleStyle=window.getComputedStyle(moveElem);
 //  To get it top Attribute values 
 var eleTop=parseInt(;
 //  Set timer to dynamically change moving objects top Attribute values 
 var t=setInterval(function(){
 //  The new top Value is paid to the moving object"px";
 //  When you hit the height of the screen, stop the timer and remove the moving object from the parent element 
 },10);// Falling speed is zero. 10 Ms whereabouts 1px
function init(){
 //  Get and set dynamically body The height of the"px";
 //  every 500 Milliseconds to create 1 Move objects and execute the move function 
 var t=setInterval(function(){

The above is all the content of this article, I hope you learn javascript programming help.

Related articles: