บทความนี้อธิบายถึงวิธีการของ JS เพื่อให้ได้เอฟเฟกต์ภาพเคลื่อนไหว Firework Web Firework สุดยอดสุดยอด แบ่งปันสำหรับการอ้างอิงของคุณ การวิเคราะห์เฉพาะมีดังนี้:
การกำหนดเอฟเฟกต์แอนิเมชั่นของเว็บดอกไม้ไฟที่ใช้งานโดยใช้ JS นั้นน่าทึ่งมากซึ่งสามารถปรับให้เข้ากับ JS เพื่อสร้างภาพเคลื่อนไหวดังกล่าว
คัดลอกรหัสดังนี้: <! 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> เอฟเฟกต์ Firework Super Cool </title>
</head>
<style type = "text/css">
.fire {display: block; ล้น: ซ่อน; ขนาดตัวอักษร: 12px; ตำแหน่ง: สัมบูรณ์};
ร่างกาย {ล้น: ซ่อน; พื้นหลัง:#000}
html {overflow: ซ่อน; พื้นหลัง:#000}
</style>
<body>
</body>
<script type = "text/javascript">
var fire = function (r, color) {
this.radius = r || 12;
this.color = color;
this.xpos = 0;
this.ypos = 0;
this.zpos = 0;
this.vx = 0;
this.vy = 0;
this.vz = 0;
this.mass = 1;
this.x = 0;
this.y = 0;
this.p = document.createElement ("Span");
this.p.className = "Fire";
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 = 150;
var fl = 250;
var vpx = 500;
var vpy = 300;
var gravity = .5;
ชั้น var = 200;
var bounce = -.8;
ตัวจับเวลา var;
var wind = ((math.floor (math.random ()*3) + 3)/10)*(math.random ()*2 - 1> 0? 1: -1)*. 25;
var wpos = 0;
var wcount;
กลับ {
init: function () {
wcount = 50 + math.floor (math.random () * 100);
สำหรับ (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.vz = math.random () * 6 - 3;
fire.vx = (math.random ()*2 - 1)*2;
fire.vy = math.random ()* - 15 - 15;
Fire.x = 500
Fire.Y = 600;
Fire.append (document.body);
-
var that = this;
timer = setInterval (ฟังก์ชัน () {
WPOS ++;
if (wpos> = wcount) {
WPOS = 0;
wcount = 50 + math.floor (math.random () * 100);
Wind = ((math.floor (math.random ()*3) + 3)/10)*(math.random ()*2 - 1> 0? 1: -1)*. 25;
-
สำหรับ (var i = 0; i <count; i ++) {
นั่นคือ move (ไฟ [i]);
-
}, 30);
-
ย้าย: ฟังก์ชั่น (ไฟ) {
Fire.vy += แรงโน้มถ่วง;
Fire.x += fire.vx;
Fire.y += Fire.vy;
Fire.vx += ลม;
Fire.SetPosition (Fire.x, Fire.Y);
if (fire.x <0 || fire.x> 1000 || fire.y <0 || fire.y> 600) {
fire.vx = (math.random ()*2 - 1)*2;
fire.vy = math.random ()* - 15 - 15;
Fire.x = 500;
Fire.Y = 600;
Fire.SetPosition (Fire.x, Fire.Y);
-
-
-
-
ดอกไม้ไฟ (). init ();
</script>
</html>
ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม JavaScript ของทุกคน