Home > Article > Web Front-end > How to use native js to create a starry sky effect
This time I will show you how to use native js to create a starry sky effect. What are the precautions for using native js to create a starry sky effect? The following is a practical case, let’s take a look.
The code is as follows: //Add the picture to change the path
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> html{height: 100%;} body{background: url(img/bg.jpg) no-repeat center top/cover;margin: 0;margin: 0;height: 100%;} //图片路径修改处 img{width: 20px;position: absolute;} </style> </head> <body> <script type="text/javascript"> //创建img function FireWorm(){ this.node=document.createElement("img"); this.node.src="img/1.jpg"; //图片路径修改处 } //确定img的位置 FireWorm.prototype.getpos=function(){ var Cw=document.documentElement.clientWidth; var Ch=document.documentElement.clientHeight; this.Left=Math.floor(Cw*Math.random()-50); this.Top=Math.floor(Ch*Math.random()-50); } //显示img位置 FireWorm.prototype.showpos=function(){ this.getpos(); this.node.style.left=this.Left+"px"; this.node.style.top=this.Top+"px"; document.body.appendChild(this.node); } //让img飞 FireWorm.prototype.fly=function(){ this.getpos(); var _this=this; startMove(this.node,{left:this.Left,top:this.Top},function(){ _this.fly(); }) } for(var i=0;i<300;i++){ var oFireWorm =new FireWorm(); oFireWorm.showpos(); oFireWorm.fly(); }</script> </body> </html>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Chinese Other related articles online!
Recommended reading:
Summary of JS inheritance method (with case)
The above is the detailed content of How to use native js to create a starry sky effect. For more information, please follow other related articles on the PHP Chinese website!