forked from motyar/firefly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.firefly.js
71 lines (63 loc) · 1.87 KB
/
jquery.firefly.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
/*
* jQuery Firefly v0.1
* https://github.com/motyar/firefly
* Licensed under the MIT license.
* Copyright 2011 Dharmveer Motyar
* http://motyar.blogspot.com
*/
(function($) {
/* Plugin defaults */
var defaults = {
images : [
'http://dharmmotyar.googlecode.com/svn/trunk/images/spark.png',
'http://dharmmotyar.googlecode.com/svn/trunk/images//spark2.png',
'http://dharmmotyar.googlecode.com/svn/trunk/images/spark3.png',
'http://dharmmotyar.googlecode.com/svn/trunk/images/spark4.png'],
total : 40
};
$.firefly = function(settings) {
$.firefly.settings = $.extend({}, defaults, settings);
if($.firefly.preloadImages()){
for (i = 0; i < $.firefly.settings.total; i++){
$.firefly.fly($.firefly.create($.firefly.settings.images[$.firefly.random(($.firefly.settings.images).length)]));
}
}
return;
};
/* Public Functions */
$.firefly.create = function(img){
spark = $('<img>').attr({'src' : img}).hide();
$(document.body).append(spark);
return spark.css({
'position':'absolute',
'z-index': $.firefly.random(20),
top: $.firefly.random(($(window).height()-150)), //offsets
left: $.firefly.random(($(window).width()-150)) //offsets
}).show();
}
$.firefly.fly = function(sp) {
$(sp).animate({
top: $.firefly.random(($(window).height()-150)), //offsets
left: $.firefly.random(($(window).width()-150)),
opacity: $.firefly.opacity()
}, (($.firefly.random(10) + 5) * 1100),function(){ $.firefly.fly(sp) } );
};
$.firefly.preloadImages = function() {
var preloads = new Object();
for (i = 0; i < ($.firefly.settings.images).length; i++){
preloads[i] = new Image(); preloads[i].src = $.firefly.settings.images[i];
}
return true;
}
$.firefly.random = function(max) {
return Math.ceil(Math.random() * max) - 1;
}
$.firefly.opacity = function()
{
op = Math.random();
if(op < .2)
return 0;
else
return 1;
}
})(jQuery);