บทความนี้อธิบายถึงวิธีการของ JavaScript เพื่อให้ได้ผลของการกระจายเกล็ดหิมะที่มีสีสันจากท้องฟ้า แบ่งปันสำหรับการอ้างอิงของคุณ การวิเคราะห์เฉพาะมีดังนี้:
มาดูเอฟเฟกต์การวิ่งก่อนดังแสดงในรูปด้านล่าง:
ซอร์สโค้ดที่สมบูรณ์มีดังนี้:
คัดลอกรหัสดังนี้: <! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title> เกล็ดหิมะสีสันสดใส JavaScript ตกลงมาจากท้องฟ้า </title>
<style type = "text/css">
.JB51SNOW {แสดง: บล็อก; ล้น: ซ่อน; ขนาดตัวอักษร: 12px; ตำแหน่ง: สัมบูรณ์};
ร่างกาย {พื้นหลัง:#000; มาร์จิ้น: 0px}
html {overflow: ซ่อน; พื้นหลัง:#000;}
A {color: White; การตกแต่งข้อความ: ไม่มี}
.jb51title {สี: สีแดง; ความสูง: 140px; ความกว้าง: 800px; มาร์จิ้น: 0px auto; text-allign: center}
</style>
</head>
<body>
<script type = "text/javascript">
var yanhua = "yanhua.vevb.com";
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 = "JB"+"51Snow";
this.p.innerhtml = "*";
this.p.style.fontsize = this.radius + "px";
this.p.style.color = "#" + this.color;
-
Fire.prototype = {
ผนวก: ฟังก์ชั่น (พาเรนต์) {
parent.appendchild (this.p);
-
setsize: ฟังก์ชั่น (มาตราส่วน) {
this.p.style.fontsize = this.radius * scale + "px";
-
SetPosition: ฟังก์ชั่น (x, y) {
this.p.style.left = x + "px";
this.p.style.top = y + "px";
-
setVisible: function (b) {
this.p.style.display = b? "บล็อก": "ไม่มี";
-
-
VAR Fireworks = function () {
var fires = new Array ();
จำนวน var = 100;
var fl = 250;
var vpx = 500;
var vpy = 300;
var gravity = .3;
ชั้น var = 200;
var bounce = -.8;
ตัวจับเวลา var;
กลับ {
init: function () {
สำหรับ (var i = 0; i <count; i ++) {
var color = 0xff0000;
color = (math.random () * 0xffffff) .tostring (16) .tostring (). แยก (".") [0];
ในขณะที่ (color.length <6) {
color = "0" + สี;
-
Var Fire = New Fire (12, Color);
FIRES.PUSH (ไฟ);
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 (ฟังก์ชัน () {
สำหรับ (var i = 0; i <count; i ++) {
นั่นคือ move (ไฟ [i]);
-
}, 30);
-
ย้าย: ฟังก์ชั่น (ไฟ) {
Fire.vy += แรงโน้มถ่วง;
Fire.xpos += fire.vx;
fire.ypos += fire.vy;
Fire.zpos += fire.vz;
if (fire.ypos> ชั้น) {
Fire.ypos = ชั้น;
Fire.vy *= ตีกลับ;
-
if (fire.zpos> -fl) {
var scale = fl / (fl + fire.zpos);
Fire.setsize (สเกล);
Fire.SetPosition (vpx + fire.xpos * มาตราส่วน
vpy + fire.ypos * มาตราส่วน);
Fire.setVisible (จริง);
} อื่น {
Fire.setVisible (เท็จ);
-
-
-
-
if (yanhua === 'yanhua.jb' + '51. ' +' net ')
ดอกไม้ไฟ (). init ();
ฟังก์ชั่น jb51snow () {
window.location.reload ();
} ถ้า (yanhua === 'yanhua.jb' + '51. ' +' net ')
setInterval (JB51Snow, 6000);
</script>
</body>
</html>
ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม JavaScript ของทุกคน