-
Notifications
You must be signed in to change notification settings - Fork 5
/
snow.js
executable file
·108 lines (87 loc) · 2.58 KB
/
snow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* jQuery snow effects.
*
* This is a heavily modified, jQuery-adapted, browser-agnostic version of
* "Snow Effect Script" by Altan d.o.o. (http://www.altan.hr/snow/index.html).
*
* Dustin Oprea (2011)
*/
function __ShowSnow(settings)
{
var snowsrc = settings.SnowImage;
var no = settings.Quantity;
var dx, xp, yp; // coordinate and position variables
var am, stx, sty; // amplitude and step variables
var i;
var doc_width = $(window).width() - 10;
var doc_height = $(window).height();
dx = [];
xp = [];
yp = [];
am = [];
stx = [];
sty = [];
flakes = [];
for (i = 0; i < no; ++i)
{
dx[i] = 0; // set coordinate variables
xp[i] = Math.random()*(doc_width-50); // set position variables
yp[i] = Math.random()*doc_height;
am[i] = Math.random()*20; // set amplitude variables
stx[i] = 0.02 + Math.random()/10; // set step variables
sty[i] = 0.7 + Math.random(); // set step variables
var flake = $("<div />");
var id = ("dot" + i);
flake.attr("id", id);
flake.css({
position: "absolute",
zIndex: i,
top: "15px",
left: "15px"
});
flake.append("<img src='" + snowsrc + "'>");
flake.appendTo("body");
flakes[i] = $("#" + id);
}
var animateSnow;
animateSnow = function()
{
for (i = 0; i < no; ++ i)
{
// iterate for every dot
yp[i] += sty[i];
if (yp[i] > doc_height - 50)
{
xp[i] = Math.random() * (doc_width - am[i] - 30);
yp[i] = 0;
stx[i] = 0.02 + Math.random() / 10;
sty[i] = 0.7 + Math.random();
}
dx[i] += stx[i];
flakes[i].css("top", yp[i] + "px");
flakes[i].css("left", (xp[i] + am[i] * Math.sin(dx[i])) + "px");
}
snowtimer = setTimeout(animateSnow, 10);
};
function hidesnow()
{
if(window.snowtimer)
clearTimeout(snowtimer)
for (i = 0; i < no; i++)
flakes[i].hide();
}
animateSnow();
if (settings.HideSnowTime > 0)
setTimeout(hidesnow, settings.HideSnowTime * 1000)
}
(function($) {
$.fn.snow = function(options) {
var settings = $.extend({
SnowImage: undefined,
Quantity: 7,
HideSnowTime: 0
}, options);
__ShowSnow(settings);
return this;
}
})(jQuery);