如何将这个javascript下雪效果放在HTML网站上? [英] How to put this javascript falling snow effect on an HTML website?

查看:71
本文介绍了如何将这个javascript下雪效果放在HTML网站上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它似乎会影响页面的其他元素,并且根本不会在Chrome和Microsoft Edge上显示。以下是javascript代码。



我尝试过:







grphcs = new Array(6)

Image0 = new Image();

Image0.src = grphcs [0] =https://www.skinnyski.com/images/flake.gif;

Image1 = new Image();

Image1.src = grphcs [1] =https://www.skinnyski.com/images/flake.gif

Image2 = new Image();

Image2.src = grphcs [2] =https://www.skinnyski.com/images/flake.gif

Image3 = new Image();

Image3.src = grphcs [ 3] =https://www.skinnyski.com/images/flake.gif

Image4 =新图像();

Image4.src = grphcs [4 ] =https://www.skinnyski.com/images/flake.gif

Image5 =新图片();

Image5.src = grphcs [5] =https://www.skinnyski.com/images/flake.gif



金额= 8; //平滑度取决于图像文件大小,尺寸越小

//你可以使用更多!

Ypos = new Array();

Xpos = new Array();

Speed = new Array();

Step = new Array();

Cstep = new Array();

ns =(document.layers)?1:0;

ns6 =(document.getElementById&& ;! document。所有)?1:0;

if(ns){

for(i = 0; i& lt; Amount; i ++){

var P = Math.floor(Math.random()* grphcs.length);

rndPic = grphcs [P];

document.write(& LT; LAYER NAME ='sn+ i +'LEFT = 0 TOP = 0>< img src =+ rndPic +>

< / LAYER>);

}

}

else {

document.write('< div style =position:absolute; top:0px ; left:0px>< div

style =position:relative>');

for(i = 0; i& lt; Amount ; i ++){

var P = Math.floor(Math.random()* grphcs.length);

rndPic = grphcs [P];

document.write('< img id =si'+ i +'src ='+ rndPic +'

style =position:absolute; top:0px; left:0px>');

}

document.write('< / div> ;< / div>');

}

WinHeight =(ns || ns6)?window.innerHeight:window.document.body.clientHeight;

WinWidth =(ns || ns6)?window.innerWidth-70:window.document.body.clientWidth;

for(i = 0;我& lt;量; i ++){

Ypos [i] = Math.round(Math.random()* WinHeight);

Xpos [i] = Math.round(Math.random( )* WinWidth);

Speed [i] = Math.random()* 5 + 3;

Cstep [i] = 0;

Step [i] = Math.random()* 0.1 + 0.05;

}

函数fall(){

var WinHeight =( NS || NS6)?

window.innerHeight:window.document.body.clientHeight;

var WinWidth =(ns || ns6)?window.innerWidth-

70:window.document.body.clientWidth;

var hscrll =(ns || ns6)?window.pageYOffset:document.body.scrollTop;

var wscrll =( ns || ns6)?window.pageXOffset:document.body.scrollLeft;

for(i = 0; i& lt; Amount; i ++){

sy =速度[i] * Math.sin(90 * Math.PI / 180);

sx =速度[i] * Math.cos(Cstep [i]);

Ypos [i] + = sy;

Xpos [i] + = sx;

if(Ypos [i]> WinHeight){

Ypos [i] = - 60;

Xpos [i] =数学。 round(Math.random()* WinWidth);

Speed [i] = Math.random()* 5 + 3;

}

如果(ns){

document.layers ['sn'+ i] .left = Xpos [i];

document.layers ['sn'+ i] .top = Ypos [i] + hscrll;

}

else if(ns6){

document.getElementById(si+ i ).style.left = Math.min(WinWidth,Xpos [i]);

document.getElementById(si+ i).style.top = Ypos [i] + hscrll;

}

else {

eval(document.all.si+ i).style.left = Xpos [i];

eval(document.all.si+ i).style.top = Ypos [i] + hscrll;

}

Cstep [i] + = Step [i];

}

setTimeout('fall()',40);

}



window.onload = fall;

// - >

It seems to affect other elements of the page and doesn't show at all on Chrome and Microsoft Edge. Below is the javascript code.

What I have tried:



grphcs=new Array(6)
Image0=new Image();
Image0.src=grphcs[0]="https://www.skinnyski.com/images/flake.gif";
Image1=new Image();
Image1.src=grphcs[1]="https://www.skinnyski.com/images/flake.gif"
Image2=new Image();
Image2.src=grphcs[2]="https://www.skinnyski.com/images/flake.gif"
Image3=new Image();
Image3.src=grphcs[3]="https://www.skinnyski.com/images/flake.gif"
Image4=new Image();
Image4.src=grphcs[4]="https://www.skinnyski.com/images/flake.gif"
Image5=new Image();
Image5.src=grphcs[5]="https://www.skinnyski.com/images/flake.gif"

Amount=8; //Smoothness depends on image file size, the smaller the size the
//more you can use!
Ypos=new Array();
Xpos=new Array();
Speed=new Array();
Step=new Array();
Cstep=new Array();
ns=(document.layers)?1:0;
ns6=(document.getElementById&&!document.all)?1:0;
if (ns){
for (i = 0; i &lt; Amount; i++){
var P=Math.floor(Math.random()*grphcs.length);
rndPic=grphcs[P];
document.write("&lt;LAYER NAME='sn"+i+"' LEFT=0 TOP=0><img src="+rndPic+">
</LAYER>");
}
}
else{
document.write('<div style="position:absolute;top:0px;left:0px"><div
style="position:relative">');
for (i = 0; i &lt; Amount; i++){
var P=Math.floor(Math.random()*grphcs.length);
rndPic=grphcs[P];
document.write('<img id="si'+i+'" src="'+rndPic+'"
style="position:absolute;top:0px;left:0px">');
}
document.write('</div></div>');
}
WinHeight=(ns||ns6)?window.innerHeight:window.document.body.clientHeight;
WinWidth=(ns||ns6)?window.innerWidth-70:window.document.body.clientWidth;
for (i=0; i &lt; Amount; i++){
Ypos[i] = Math.round(Math.random()*WinHeight);
Xpos[i] = Math.round(Math.random()*WinWidth);
Speed[i]= Math.random()*5+3;
Cstep[i]=0;
Step[i]=Math.random()*0.1+0.05;
}
function fall(){
var WinHeight=(ns||ns6)?
window.innerHeight:window.document.body.clientHeight;
var WinWidth=(ns||ns6)?window.innerWidth-
70:window.document.body.clientWidth;
var hscrll=(ns||ns6)?window.pageYOffset:document.body.scrollTop;
var wscrll=(ns||ns6)?window.pageXOffset:document.body.scrollLeft;
for (i=0; i &lt; Amount; i++){
sy = Speed[i]*Math.sin(90*Math.PI/180);
sx = Speed[i]*Math.cos(Cstep[i]);
Ypos[i]+=sy;
Xpos[i]+=sx;
if (Ypos[i] > WinHeight){
Ypos[i]=-60;
Xpos[i]=Math.round(Math.random()*WinWidth);
Speed[i]=Math.random()*5+3;
}
if (ns){
document.layers['sn'+i].left=Xpos[i];
document.layers['sn'+i].top=Ypos[i]+hscrll;
}
else if (ns6){
document.getElementById("si"+i).style.left=Math.min(WinWidth,Xpos[i]);
document.getElementById("si"+i).style.top=Ypos[i]+hscrll;
}
else{
eval("document.all.si"+i).style.left=Xpos[i];
eval("document.all.si"+i).style.top=Ypos[i]+hscrll;
}
Cstep[i]+=Step[i];
}
setTimeout('fall()',40);
}

window.onload=fall;
//-->

推荐答案

这是一个真古剧本:
That is a really ancient script:
ns=(document.layers)?1:0;
ns6=(document.getElementById&&!document.all)?1:0;



你应该寻找一个更新的脚本,提供类似 GitHub - scottschiller / Snowstorm:互联网的企业级JavaScript雪景效应,自2003年以来每年冬天都在全球范围内设置CPU。 [ ^ ]。


我做了一些更改。但图像没有移动。我会尝试另一种方法



I made some changes .But the image not moved .I will try in another method

<html>
<head id="Head1" runat="server">
    <title></title>
   
    
        
        

<script>
 
var grphcs=new Array(6);
var Image0=new Image();
 Image0.src=grphcs[0]="https://www.skinnyski.com/images/flake.gif";
var Image1=new Image();
Image1.src=grphcs[1]="https://www.skinnyski.com/images/flake.gif"
var Image2=new Image();
Image2.src=grphcs[2]="https://www.skinnyski.com/images/flake.gif"
var Image3=new Image();
Image3.src=grphcs[3]="https://www.skinnyski.com/images/flake.gif"
var Image4=new Image();
Image4.src=grphcs[4]="https://www.skinnyski.com/images/flake.gif"
var Image5=new Image();
Image5.src=grphcs[5]="https://www.skinnyski.com/images/flake.gif" 
var i =0;
var Amount=8;
var Ypos=new Array(); 
var Xpos=new Array();
var Speed=new Array();
var Step=new Array();
var Cstep=new Array();
ns=(document.layers)?1:0;
ns6=(document.getElementById&&!document.all)?1:0;
if(ns==0){

for (i = 0; i < Amount; i++){
var P=Math.floor(Math.random()*grphcs.length);
var rndPic=grphcs[P];

document.write("<LAYER Id='sn"+i+"' LEFT=50 TOP=50><img src="+rndPic+"></LAYER>");
}
}
else
{
document.write("<div style=position:absolute;top:0px;left:0px><div style=position:relative>");


for (i = 0; i < Amount; i++){
var P=Math.floor(Math.random()*grphcs.length);
var rndPic=grphcs[P];
document.write("<img id=si"+i+" src="+rndPic+" style=position:absolute;top:0px;left:0px>");
}
document.write("</div></div>");

}
WinHeight=(ns||ns6)?window.innerHeight:window.document.body.clientHeight;
WinWidth=(ns||ns6)?window.innerWidth-70:window.document.body.clientWidth;
for (i=0; i < Amount; i++){ 
Ypos[i] = Math.round(Math.random()*WinHeight);
Xpos[i] = Math.round(Math.random()*WinWidth);
Speed[i]= Math.random()*5+3;
Cstep[i]=0;
Step[i]=Math.random()*0.1+0.05;
}

function fall(){

var WinHeight=(ns==ns6)?window.innerHeight:window.document.body.clientHeight;
var WinWidth=(ns==ns6)?window.innerWidth-70:window.document.body.clientWidth;
var hscrll=(ns==ns6)?window.pageYOffset:document.body.scrollTop;
var wscrll=(ns==ns6)?window.pageXOffset:document.body.scrollLeft;
for (i=0; i < Amount; i++){

var sy = Speed[i]*Math.sin(90*Math.PI/180);
var sx = Speed[i]*Math.cos(Cstep[i]);

Ypos[i]+=sy;
Xpos[i]+=sx; 

if (Ypos[i] > WinHeight){
Ypos[i]=-60;
Xpos[i]=Math.round(Math.random()*WinWidth);
Speed[i]=Math.random()*5+3;

}


if (ns==0){
var lnm = "sn"+i;

var lar = document.getElementById(lnm);

lar.style.left = 200;

//document.layers['sn'+i].left=Xpos[i];
//document.layers['sn'+i].top=Ypos[i]+hscrll;
}
else if (ns6==0){
document.getElementById("si"+i).style.left=Math.min(WinWidth,Xpos[i]);
document.getElementById("si"+i).style.top=Ypos[i]+hscrll;
}
else{
eval("document.all.si"+i).style.left=Xpos[i];
eval("document.all.si"+i).style.top=Ypos[i]+hscrll;
} 
Cstep[i]+=Step[i];
}

setTimeout('fall()',40);
}
   
</script>

</head>


<body onload="fall();">
    
<div>
</div>
</body>
</html>


这篇关于如何将这个javascript下雪效果放在HTML网站上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆