好的,所以我有一个雪花落在屏幕上的小动画,我曾经setTimeout
让它移动。我喜欢它目前的样子(至少在 Chrome 上),现在,我正在研究交叉兼容性,我正在研究 Firefox,它的运行速度非常缓慢。老实说,该setTimeout
函数设置为 5ms,它每 5ms 仅移动 1px(速度为 200 fps),但我需要它以那个速度运行。是否有一些狡猾的技巧可以绕过它,Firefox 以什么速度重复该功能?另外,旁注:它甚至在 IE9 中都不起作用(我不关心 <8)。他们移动了一秒钟,到达屏幕底部,然后停止(我希望它在顶部重新启动)。
任何帮助将不胜感激!
我的代码(如果你想通过它)如果你不跳到"//begin actual function"
:
var width = window.outerWidth;
var height = window.outerHeight;
//foreground
var curWidth = width - 90;
var h2 = height * 5;
var tbvar;
function TB() {
//random flake
var flakeSrc = ['images/1.png','images/2.png','images/3.png','images/4.png'];
var randImg1 = Math.floor(Math.random() * flakeSrc.length );
var randImg2 = Math.floor(Math.random() * flakeSrc.length );
var randImg3 = Math.floor(Math.random() * flakeSrc.length );
var randImg4 = Math.floor(Math.random() * flakeSrc.length );
var randImg5 = Math.floor(Math.random() * flakeSrc.length );
//random horizontal position array
var left = [];
var one = 1;
do {
one++;
left.push(one)
}
while (one <= curWidth);
var rand1 = Math.floor(Math.random() * left.length );
var rand2 = Math.floor(Math.random() * left.length );
var rand3 = Math.floor(Math.random() * left.length );
var rand4 = Math.floor(Math.random() * left.length );
var rand5 = Math.floor(Math.random() * left.length );
//////////////begin actual function/////////////////////////////////////////////
clearTimeout(tbvar);
tbvar=setTimeout(function(){
//find top positions
var top1 = document.getElementById("flake1").offsetTop;
var top2 = document.getElementById("flake2").offsetTop;
var top3 = document.getElementById("flake3").offsetTop;
var top4 = document.getElementById("flake4").offsetTop;
var top5 = document.getElementById("flake5").offsetTop;
//add 1
top1++;
top2++;
top3++;
top4++;
top5++;
//change top positions
document.getElementById("flake1").style.top = top1 + "px";
document.getElementById("flake2").style.top = top2 + "px";
document.getElementById("flake3").style.top = top3 + "px";
document.getElementById("flake4").style.top = top4 + "px";
document.getElementById("flake5").style.top = top5 + "px";
//end of screen statements
if (top1 == height) {
//top1 == "-180px";
document.getElementById("flake1").style.top = "-180px";
document.getElementById("flake1").style.left = rand1 + "px";
document.getElementById('flake1').src = flakeSrc[randImg1];
}
if (top2 == height) {
//top2 == "-180px";
document.getElementById("flake2").style.top = "-180px";
document.getElementById("flake2").style.left = rand2 + "px";
document.getElementById('flake2').src = flakeSrc[randImg2];
}
if (top3 == height) {
//top3 == "-180px";
document.getElementById("flake3").style.top = "-180px";
document.getElementById("flake3").style.left = rand3 + "px";
document.getElementById('flake3').src = flakeSrc[randImg3];
}
if (top4 == height) {
//top4 == "-180px";
document.getElementById("flake4").style.top = "-180px";
document.getElementById("flake4").style.left = rand4 + "px";
document.getElementById('flake4').src = flakeSrc[randImg4];
}
if (top5 == height) {
//top5 == "-180px";
document.getElementById("flake5").style.top = "-180px";
document.getElementById("flake5").style.left = rand5 + "px";
document.getElementById('flake5').src = flakeSrc[randImg5];
}
TB();
}, 5);
}
TB();
如果您想查看整个项目的实际效果:http: //fudgepants.com/snow/snow.html
我说的是大雪花,而不是小雪花。
谢谢!