Cet article décrit la méthode de JavaScript pour obtenir l'effet de la diffusion des flocons de neige colorés du ciel. Partagez-le pour votre référence. L'analyse spécifique est la suivante:
Jetons un coup d'œil à l'effet de course d'abord, comme indiqué dans la figure ci-dessous:
Le code source complet est le suivant:
Copiez le code comme suit: <! 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">
<adal>
<Title> JavaScript Les flocons de neige colorés tombent du ciel </Title>
<style type = "text / css">
.jb51Snow {affichage: bloc; débordement: caché; taille de police: 12px; position: absolu};
corps {fond: # 000; marge: 0px}
html {overflow: Hidden; Contexte: # 000;}
A {couleur: blanc; text-décoration: aucun}
.JB51Title {Color: Red; Hauteur: 140px; largeur: 800px; marge: 0px auto; Text-Align: Centre}
</ style>
</ head>
<body>
<script type = "text / javascript">
var yanhua = "yanhua.vevb.com";
var feu = fonction (r, couleur) {
this.radius = r || 12
this.color = couleur || "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;
}
Feu.prototype = {
Ajouter: fonction (parent) {
Parent.ApendChild (this.p);
},
setSize: fonction (échelle) {
this.p.style.fontsize = this.radius * échelle + "px";
},
setPosition: fonction (x, y) {
this.p.style.left = x + "px";
this.p.style.top = y + "px";
},
setVisible: fonction (b) {
this.p.style.display = b? "Block": "Aucun";
}
}
var Fireworks = function () {
var feux = new Array ();
Var Count = 100;
var fl = 250;
var vpx = 500;
var vpy = 300;
Var Gravity = .3;
Var Floor = 200;
var Bounce = -.8;
var temporisateur;
retour {
init: function () {
pour (var i = 0; i <count; i ++) {
var color = 0xff0000;
color = (math.random () * 0xffffff) .toString (16) .toString (). Split (".") [0];
while (colore.length <6) {
color = "0" + couleur;
}
Var Fire = nouveau feu (12, couleur);
Fires.push (incendie);
feu.ypos = -100;
feu.vx = math.random () * 6 - 3;
feu.vy = math.random () * 6 - 3;
feu.vz = math.random () * 6 - 3;
Fire.APPEND (document.body);
}
var that = this;
timer = setInterval (function () {
pour (var i = 0; i <count; i ++) {
that.move (feux [i]);
}
}, 30);
},
Déplacer: fonction (feu) {
Fire.vy + = Gravity;
feu.xpos + = feu.vx;
feu.ypos + = feu.vy;
feu.zpos + = feu.vz;
if (fire.ypos> plancher) {
feu.ypos = plancher;
Fire.vy * = rebond;
}
if (fire.zpos> -fl) {
var échelle = fl / (fl + fire.zpos);
Fire.SetSize (échelle);
Fire.SetPosition (VPX + Fire.xpos * Échelle,
Échelle VPY + FIRE.YPOS *);
Fire.setVisible (true);
} autre {
Fire.setVisible (false);
}
}
}
}
if (yanhua === 'yanhua.jb' + '51. '+' net ')
Fireworks (). init ();
fonction jb51snow () {
window.location.reload ();
} if (yanhua === 'yanhua.jb' + '51. '+' net ')
setInterval (JB51Snow, 6000);
</cript>
</docy>
</html>
J'espère que cet article sera utile à la programmation JavaScript de tous.