I'm having some troubles in my javascript program. I'm trying to do a java script function that makes little dots fall creating the illusion of snow.
I want 2 things that I have no clue how to do:
- I want to make the snow come down with some time spacing it. I mean fst comes down a dot the another, then another ...
- I want that when a dot touches the end of the page it resets and falls down again
Take a look at my code:
<!-- begin snippet: js hide: false --> <!-- language: lang-js -->var x = [];
var y = -20;
var yplus = [];
var xplus = []; // Variables here.
function fallstart() { // sets the snow position in the begining
var btn = document.getElementsByClassName("snow");
for (i = 0; i < x.length; i++) {
btn[i].style.left = x[i] + "px";
btn[i].style.top = y + "px";
}
}
function fall() { // This funtion updates the snow postion
var btn = document.getElementsByClassName("snow");
y = y + 2;
for (i = 0; i < x.length; i++) {
x[i] = x[i] + xplus[i];
btn[i].style.left = x[i] + "px";
btn[i].style.top = y + "px";
}
}
function keep() { // This funtion makes the snow fall and is the funtion that is called
var btn = document.getElementsByClassName("snow");
for (var i = 0; i < btn.length; i++) {
var rnd = 1280 * Math.random();
x.push(rnd);
}
for (var i = 0; i < btn.length; i++) {
var xacr = Math.random();
xplus.push(xacr);
}
for (var i = 0; i < btn.length; i++) {
var yacr = Math.random();
yplus.push(yacr);
}
fallstart();
setInterval(fall, 20);
}
<!-- language: lang-html -->
<body onload="keep()">
<div class="snow" style="color: red; position: absolute; width: 8px; height: 8px; top:-3px; left:0px"> • </div>
<div class="snow" style="color: red; position: absolute; width: 8px; height: 8px; top:-3px; left:0px"> • </div>
<div class="snow" style="color: red; position: absolute; width: 8px; height: 8px; top:-3px; left:0px"> • </div>
<div class="snow" style="color: red; position: absolute; width: 8px; height: 8px; top:-3px; left:0px"> • </div>
<div class="snow" style="color: red; position: absolute; width: 8px; height: 8px; top:-3px; left:0px"> • </div>
<div class="snow" style="color: red; position: absolute; width: 8px; height: 8px; top:-3px; left:0px"> • </div>
<div class="snow" style="color: red; position: absolute; width: 8px; height: 8px; top:-3px; left:0px"> • </div>
<div class="snow" style="color: red; position: absolute; width: 8px; height: 8px; top:-3px; left:0px"> • </div>
<div class="snow" style="color: red; position: absolute; width: 8px; height: 8px; top:-3px; left:0px"> • </div>
</body>
<!-- end snippet -->