首頁  >  文章  >  web前端  >  JS實現飄落星星動畫效果

JS實現飄落星星動畫效果

php中世界最好的语言
php中世界最好的语言原創
2018-04-19 10:33:052402瀏覽

這次帶給大家JS實現飄落星星動畫效果,JS實現飄落星星動畫效果的注意事項有哪些,下面就是實戰案例,一起來看一下。

<!DOCTYPE html>
<htmllang="en">
<head>
 <metacharset="UTF-8">
 <title>Title</title>
 <style>
  img{
   position: absolute;
  }
  body {
   background-image: url(img/bg.jpg);
   background-size: 100%;
  }
 </style>
 <script>
  function Star() {
   this.speed=10;
   this.img=new Image();
   this.img.src="img/star"+parseInt(Math.random()*4+1)+".png";
   this.img.style.width=50+'px';
   this.img.style.height=50+'px';
   this.img.style.top=Math.random()*window.innerHeight+1+'px';
   this.img.style.left=Math.random()*window.innerWidth+1+'px';
   document.body.appendChild(this.img);
  }
  Star.prototype.slip=function () {
   var that=this;
   function move() {
    that.img.style.top=that.img.offsetTop+that.speed+'px';
    console.log(that.img.offsetTop+"star");
    console.log(window.innerHeight+"window");
    if(that.img.offsetTop>window.innerHeight){
     clearInterval(sh);
     that.img.style.height=0;
     that.img.style.width=0;
    }
   }
   var sh=setInterval(move,100);
  }
   setInterval(function () {
    for(var i=1;i<5;i++){
    new Star().slip();
    }
   },1000)
 </script>
<body>
</body>
</html>

                                     

相信本文之後之後之後讓您進行了精彩文章,更多精彩文章閱讀了更多精彩文章!

推薦閱讀:

JS實作資料驗證與複選框表單提交

vue.js雙向綁定使用詳解

JS輕鬆實現輪播圖

以上是JS實現飄落星星動畫效果的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn