DIV CSS 佈局教程網

js實現宇宙星空飛行的背景動畫效果
編輯:JavaScript綜合知識     

本文分享一個用原生JavaScript實現的宇宙星空背景效果:大量小星星從螢幕中心向四周飛散,營造出在星空中高速飛行的視錯覺,很適合作為動態背景。核心是利用隨機函數決定每顆星的起始位置與方向,再用計時器不斷更新它們的位置與大小。

完整程式碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<TITLE>宇宙星空背景效果,Js特效</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.star {
  position:absolute;
  layer-background-color:white;
  visibility:visible;
  top:-50px;
  width:50px;
  height:50px;
  font-size:1px;
  background-color:white;
}
.60pt{font-size:60pt;color:#ff66cc;}
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
var starnum = 75; // 星星的數目
var isNS = (document.layers);
var _all = (isNS)? '' : 'all.';
var _style = (isNS) ? '' : '.style';
var xoffset, yoffset, w_x, w_y, tmpx, tmpy, scrlx, scrly;
function getstartpos(obj) { // 定義星星移動的起始位置
  obj.deltay = Math.floor(Math.random() * 12);
  obj.deltax = Math.floor(Math.random() * 12);
  obj.xdir = (Math.floor(Math.random() * 2) == 1) ? '+' : '-';
  obj.ydir = (Math.floor(Math.random() * 2) == 1) ? '+' : '-';
  obj.counter = 1;
  if (isNS) {
    obj.clip.width = 1;
    obj.clip.height = 1;
    obj.moveTo(xoffset+pageXOffset, yoffset+pageYOffset);
  } else {
    obj.width = 1;
    obj.height = 1;
    obj.pixelTop = yoffset+document.body.scrollTop;
    obj.pixelLeft = xoffset+document.body.scrollLeft;
  }
}
function movestar(starN) // 移動星星的位置
{
  tmpx = starN.deltax*starN.counter+starN.counter;
  tmpy = starN.deltay*starN.counter+starN.counter;
  if (isNS) {
    starN.clip.width = starN.counter / 3;
    starN.clip.height = starN.counter / 3;
    scrlx = pageXOffset;
    scrly = pageYOffset;
    if ((starN.left+tmpx >= w_x+scrlx) || (starN.top+tmpy >= w_y+scrly) \
      || (starN.left-tmpx <= scrlx) || (starN.top-tmpy <= scrly)) {
      getstartpos(starN);
    } else {
      eval('starN.moveBy('+starN.xdir+tmpx+', '+starN.ydir+tmpy+')');
    }
  } else {
    starN.width = starN.counter/3;
    starN.height = starN.counter/3;
    scrlx = document.body.scrollLeft;
    scrly = document.body.scrollTop;
    if ((starN.pixelLeft+tmpx >= w_x+scrlx)||(starN.pixelTop+tmpy >= w_y+scrly) \
      || (starN.pixelLeft-tmpx <= scrlx)||(starN.pixelTop-tmpy <= scrly)) {
      getstartpos(starN);
    } else {
      eval('starN.pixelTop'+starN.ydir+'=tmpy');
      eval('starN.pixelLeft'+starN.xdir+'=tmpmpx');
    }
  }
  starN.counter++;
}
function animate() // 讓所有的星星動起來
{
  for(i=1; i <= starnum; i++) {
    movestar(eval('star'+i));
  }
  setTimeout('animate()', 100);
}

function findwindowparams() { // 定義星星移動的起始位置
  w_x = (isNS) ? window.innerWidth : document.body.clientWidth;
  w_y = (isNS) ? window.innerHeight : document.body.clientHeight;
  xoffset = w_x / 2;
  yoffset = w_y / 2;
  for (i = 1; i <= starnum; i++) {
    getstartpos(eval('star'+i));
  }
}
function resizeNS() {
  setTimeout('document.location.reload()', 400);
}
(isNS) ? window.onresize = resizeNS : window.onresize = findwindowparams;
window.onload = new Function("findwindowparams(); animate();");
//-->
</script>
</HEAD>
<BODY>
<body bgcolor=#999999>
<SCRIPT LANGUAGE="JavaScript">
<!--
for (i = 1; i <= starnum; i++) { // 給星星定義層
  document.writeln('<div id="star'+i+'" class="star"></div>');
  eval('var star'+i+'=document.'+_all+'star'+i+_style);
}
//-->
</script>
<p><center>
<font class=60pt><br>飛翔在<br>宇宙的星空中<br></font>
</center><p>
</body>
</html>

實現原理可以這樣理解:starnum控制星星數量,getstartpos用隨機函數為每顆星分配方向和步長,並把它們統一起點設在螢幕中心;movestar在每一幀裡根據counter遞增逐步放大星星尺寸並沿各自方向移動,一旦飛出邊界就重新回到中心;animate則用setTimeout每100毫秒調用一次,形成連續動畫。counter越大星星越大、位移越遠,正好模擬出由遠及近的飛行縱深感。

運行效果如下圖所示:

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved