Hallo,
closure natürlich. Leider gehts jetzt nicht in der Browserzeile so. Sagt er, sinusTable[_self.yPos] wäre nicht definiert, was scheinbar heißt, dass er das "_self", den closure, vergisst?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<script type="text/javascript">
[code lang=javascript]
javascript:
sinusTable = new Array;
createSinusTable = function () {
for (k=-30;k<screen.height;k++) {
sinusTable[k] = 2*Math.sin(k/5);
}
};
Snowflake = function (xPosition) {
this.star=document.createTextNode("*");
/*put it in surrounding div, whiten it and append*/
this.div=document.createElement("div");
this.div.appendChild(this.star);
this.div.style.color="#fff";
DB.appendChild(this.div);
/* size and base-position and store for each snowflake*/
this.fontSize = Math.ceil(Math.random()*40);
this.div.style.fontSize=this.fontSize + "px";
this.div.style.position="absolute";
this.yPos=-this.fontSize;
this.xPos = xPosition;
this.div.style.left=10 + this.xPos + "px";
this.div.style.top= this.yPos+"px";
/* closure - capture/close this in private var*/
var _self = this;
this.fall = function(j) {
if (_self.yPos < myMarginBottom) {
_self.yPos += myPixelsPerStep;
/* new postition, down and left/right for little sinus-swing*/
_self.div.style.top = _self.yPos + "px";
_self.div.style.left = _self.xPos + sinusTable[_self.yPos] + "px";
} else {
_self.yPos=-_self.fontSize;
}
/* to call in setTimeout-function, will be e.g. window[snow40](40)*/
setTimeout(_self.fall,myTimeout);
};
};
window.onload = function() {
createSinusTable();
DB=document.body;
DB.style.backgroundColor = "#000";
myStartMarginLeft = 40;
myDistanceBetweenFlakes = 10;
/* stop before bottom of window*/
myMarginBottom = screen.height - 150;
/* pixels to move for each step;*/
myPixelsPerStep = 1;
/* speed in msec*/
myTimeout = 10;
/* walk from left margin to end of screen*/
mySnowflakes = new Array;
for (i = myStartMarginLeft; i< screen.width; i += myDistanceBetweenFlakes) {
/*make a snowchristal*/
mySnowflakes[i] = new Snowflake(i);
functionCallNow = 'mySnowflakes['+i+'].fall()';
/* start random*/
setTimeout(functionCallNow,Math.random()*50000);
}
};
void(0)
;
</script>
</head>
<body>
</body>
</html>
[/code]
Gruß
jobo