It's snowing. JavaScript makes snowflakes dance_javascript skills
The example in this article shares the JavaScript implementation of the snowflakes flying effect for your reference. The specific content is as follows
Principle:
1. Dynamically create DIV with js, specify the CLASS class to set different background image styles to display different snowflake effects.
2. js gets the created DIV and changes its top attribute value. When the height of the drop is greater than the screen height, delete the mobile div
3. If it seems not perfect enough, please don’t spray
Effect preview:http://wjf444128852.github.io/demo02/snow/index.html
HTML code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>雪花飞舞</title> <link rel="stylesheet" href="css/index.css"> <script src="js/move.js"></script> </head> <body> <div class="snow_parent" id="js_sonw"> </div> </body> </html>
CSS code
*{ margin:0; padding:0; list-style: none; border: none; } body{ width: 100%; height:600px; background:#000; } .snow_parent{ position: relative; width: 100%; height: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; } .snow_one{ width: 180px; height: 180px; background-position:0 0; background-repeat: no-repeat; left:-70px; top: -95px; } .snow_two{ width: 140px; height: 140px; background-position:-220px -18px; left:-30px; top: -75px; } .snow_three{ width:150px; height: 150px; background-position:-400px -15px; left:-20px; top: -80px; } .snow_four{ width: 160px; height: 160px; background-position:-10px -206px; } .snow_four{ left:-10px; top: -85px; }
JS code:
/* creatBy jiucheng 2016-4-24 */ window.onload=function(){ init(); } // 创建DIV function creatDiv(){ // 创建DIV并追加到父元素 var snowDiv=document.createElement("div"); document.getElementById("js_sonw").appendChild(snowDiv); // 让创建DIV的class为随机,显示不同的雪花 var whatName=["snow_one parent","snow_two parent","snow_three parent","snow_four parent"]; var index=Math.floor(Math.random()*whatName.length); snowDiv.className=whatName[index]; // 获取该DIV的left属性值(随机的)并赋值给创建的DIV var whatLeft=getLeft()+'px'; snowDiv.style.left=whatLeft; return snowDiv; } // 获取随机left属性值 function getLeft(){ // 获取该DIV的最大left属性值即父元素的宽度 var eleParent=document.getElementById("js_sonw"); // 获取父元素的所有style样式 var style=window.getComputedStyle(eleParent); // CSS中的left是负数这里得减去下 var maxWidth=parseInt(style.width)+70; // 让创建的DIV的left为随机值 var randomLeft=Math.floor(Math.random()*maxWidth); return randomLeft; } // 让其向下移动 function moveDown(){ // 获取移动对象 var moveElem=creatDiv(); // 获取移动对象的所有style属性值 var eleStyle=window.getComputedStyle(moveElem); // 获取它的top属性值 var eleTop=parseInt(eleStyle.top); // 设置定时器动态改变移动对象的top属性值 var t=setInterval(function(){ eleTop++; // 把新的top值付给移动对象 moveElem.style.top=eleTop+"px"; // 当下落到屏幕的高度后停止定时器并把该移动对象从父元素删除 if(eleTop>=window.innerHeight){ clearInterval(t); document.getElementById("js_sonw").removeChild(moveElem); } },10);//下落速度没10毫秒下落1px } function init(){ // 动态获取并设置body的高度 document.body.style.height=window.innerHeight+"px"; // 每500毫秒创建一个移动对象并执行移动函数 var t=setInterval(function(){ moveDown(); },100); }
The above is the entire content of this article. I hope it will be helpful to everyone in learning javascript programming.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

This article explores effective use of Java's Collections Framework. It emphasizes choosing appropriate collections (List, Set, Map, Queue) based on data structure, performance needs, and thread safety. Optimizing collection usage through efficient

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript
