DIV CSS 佈局教程網

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>Javascript 多彩雪花從天降-</title>
<style type="text/css">
.KeleyiSnow{display:block; overflow:hidden; font-size:12px; position:absolute};
body{background:#000;margin:0px}
html{overflow:hidden; background:#000;}
a{color:White;text-decoration:none}
.keleyiTitle{color:red;height:140px;width:800px;margin:0px auto;text-align:center}
</style>
</head>

<body>

<script type="text/javascript">
 var yanhua = "yanhua.";
 var Fire = function (r, color) {
 this.radius = r || 12;
 this.color = color || "FF6600";
 this.xpos = 0;
 this.ypos = 0;
 this.zpos = 0;
 this.vx = 0;
 this.vy = 0;
 this.vz = 0;
 this.mass = 1;
 this.p = document.createElement("span");
 this.p.className = "Ke"+"leyiSnow";
 this.p.innerHTML = "*";
 this.p.style.fontSize = this.radius + "px";
 this.p.style.color = "#" + this.color;
 }
 Fire.prototype = {
 append: function (parent) {
 parent.appendChild(this.p);
 },
 setSize: function (scale) {
 this.p.style.fontSize = this.radius * scale + "px";
 },
 setPosition: function (x, y) {
 this.p.style.left = x + "px";
 this.p.style.top = y + "px";
 },
 setVisible: function (b) {
 this.p.style.display = b ? "block" : "none";
 }
 }
 var fireworks = function () {
 var fires = new Array();
 var count = 100;
 var fl = 250;
 var vpx = 500;
 var vpy = 300;
 var gravity = .3;
 var floor = 200;
 var bounce = -.8;
 var timer;
 return {
 init: function () {
 for (var i = 0; i < count; i++) {
 var color = 0xFF0000;
 color = (Math.random() * 0xFFFFFF).toString(16).toString().split(".")[0];
 while (color.length < 6) {
 color = "0" + color;
 }
 var fire = new Fire(12, color);
 fires.push(fire);
 fire.ypos = -100;
 fire.vx = Math.random() * 6 - 3;
 fire.vy = Math.random() * 6 - 3;
 fire.vz = Math.random() * 6 - 3;
 fire.append(document.body);
 }
 var that = this;
 timer = setInterval(function () {
 for (var i = 0; i < count; i++) {
 that.move(fires[i]);
 }
 }, 30);
 },
 move: function (fire) {
 fire.vy += gravity;
 fire.xpos += fire.vx;
 fire.ypos += fire.vy;
 fire.zpos += fire.vz;
 if (fire.ypos > floor) {
 fire.ypos = floor;
 fire.vy *= bounce;
 }
 if (fire.zpos > -fl) {
 var scale = fl / (fl + fire.zpos);
 fire.setSize(scale);
 fire.setPosition(vpx + fire.xpos * scale,
vpy + fire.ypos * scale);
 fire.setVisible(true);
 } else {
 fire.setVisible(false);
 }
 }
 }
 }
 if (yanhua === 'yanhua.ke' + 'leyi.' + 'com')
 fireworks().init();

 function KeleyiSnow() {
 window.location.reload();
 } if (yanhua === 'yanhua.ke' + 'leyi.' + 'com')
 setInterval(KeleyiSnow, 6000);
</script>
<div class="keleyiTitle"><h2>多彩雪花從天降·</h2><a href="https:///a/bjac/flyuqb6r.htm" target="_blank">原文</a> 請最大化窗口查看</div>
</body>
</html>

實現原理上,Fire 類負責創建單個雪花元素並管理其位置、大小與可見性;fireworks 通過 setInterval 每 30 毫秒批量更新所有雪花的位置,模擬出重力下落與彈跳效果,進而呈現出繽紛飄落的視覺體驗。

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