js如何实现漂亮的星空背景

这篇文章给大家分享的是有关js如何实现漂亮的星空背景的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

成都创新互联成立与2013年,公司以成都网站建设、网站建设、系统开发、网络推广、文化传媒、企业宣传、平面广告设计等为主要业务,适用行业近百种。服务企业客户1000多家,涉及国内多个省份客户。拥有多年网站建设开发经验。为企业提供专业的网站建设、创意设计、宣传推广等服务。 通过专业的设计、独特的风格,为不同客户提供各种风格的特色服务。

具体内容如下

html代码:

css代码

 html, body {
 height: 100%;
 overflow: hidden;
 }

 body {
 width: 100%;
 height:100%;
 background: #000;
 background-size: 100%;
 perspective: 500px;

 }
 .stars {
 position: absolute;
 top: 50%;
 left: 50%;
 width: 4px;
 height:4px;
 -webkit-border-radius:50%;
 -moz-border-radius:50%;
 border-radius:50%;
 animation: fly 2s linear infinite;
 transform-style: preserve-3d;
 }
 .stars:before, .stars:after {
 content: "";
 position: absolute;
 width: inherit;
 height: inherit;
 box-shadow: inherit;
 }
 .stars:before {
 transform: translateZ(-300px);
 opacity: .6;
 }
 .stars:after {
 transform: translateZ(-600px);
 opacity: .4;
 }

 @keyframes fly {
 from {
 transform: translateZ(0px);
 opacity: .6;
 }
 to {
 transform: translateZ(300px);
 opacity: 1;
 }
 }

js代码 

var w = document.documentElement.clientWidth*1.2;
var h = document.documentElement.clientHeight*1.2;
var star = document.getElementsByClassName("stars")[0];
var n = 1000;
 //随机函数
 function randomNum(m, n) {
 return Math.floor(Math.random() * (n - m + 1) + m);
 }
 var str = '';
 for (var i = 0; i < n; i++) {
 var numX = randomNum(-w, w);
 var numY = randomNum(-h, h);
 var color = 'rgb(' + randomNum(0, 255) + ',' + randomNum(0, 255) + ',' + randomNum(0, 255) + ')';
 str += numX +'px'+' ' + numY+'px'+' '+ color+',';
 }
 str = str.slice(0,-1);
 star.style.boxShadow = str;


  $(function(){

   $('#main').fadeOut();

   $('#main').fadeIn('slow');

 })

感谢各位的阅读!关于“js如何实现漂亮的星空背景”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!


新闻名称:js如何实现漂亮的星空背景
分享路径:http://pwwzsj.com/article/gjedhe.html

其他资讯