-
Notifications
You must be signed in to change notification settings - Fork 56
/
jquery.firefly-0.3.js
125 lines (101 loc) · 2.79 KB
/
jquery.firefly-0.3.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* jQuery firefly plugin 0.3
*
*
* Copyright (c) 2010 Dharmveer Motyar
* http://motyar.blogspot.com
*
* $Id$
*
* licensed under the MIT licenses:
* http://www.opensource.org/licenses/mit-license.php
*
*
* Creates a firefly effect.
*
* @example $.firefly();
*
* @name firefly
* @type jQuery
* @cat Plugins/firefly
*/
(function($) {
/*
* Plugin defaults
*/
var defaults = {
total : 10,
ofTop: 0,
ofLeft: 0,
on:'document.body',
twinkle: 0.2,
minPixel: 1,
maxPixel: 2,
color: '#fff'
};
$.firefly = function(settings) {
$.firefly.settings = $.extend({}, defaults, settings);
$.firefly.eleHeight = $($.firefly.settings.on).height();
$.firefly.eleWidth = $($.firefly.settings.on).width();
if($.firefly.settings.on!=='document.body'){
var off = $($.firefly.settings.on).offset();
$.firefly.offsetTop = off.top;
$.firefly.offsetLeft = off.left;
$.firefly.eleHeight = $($.firefly.settings.on).height();
$.firefly.eleWidth = $($.firefly.settings.on).width();
}
else{
$.firefly.offsetTop = 0;
$.firefly.offsetLeft = 0;
$.firefly.eleHeight = $(document.body).height();
$.firefly.eleWidth = $(document.body).width();
}
for (i = 0; i < $.firefly.settings.total; i++){
$.firefly.fly($.firefly.create($.firefly.randomPixel($.firefly.settings.minPixel, $.firefly.settings.maxPixel)));
}
return;
};
/*
* Public Functions
*/
$.firefly.create = function(pixelSize){
spark = $('<div>').hide();
if($.firefly.settings.on === 'document.body')
$(document.body).append(spark);
else
$($.firefly.settings.on).append(spark);
return spark.css({'position':'absolute',
'width' : pixelSize,
'height': pixelSize,
'background-color': $.firefly.settings.color,
'z-index': $.firefly.random(20), //under all the stuff
top: $.firefly.offsetTop + $.firefly.random(($.firefly.eleHeight-50)), //offsets
left: $.firefly.offsetLeft + $.firefly.random(($.firefly.eleWidth-50)) //offsets
}).show();
}
$.firefly.fly = function(sp) {
$(sp).animate({
top: $.firefly.offsetTop + $.firefly.random(($.firefly.eleHeight-50)), //offsets
left: $.firefly.offsetLeft + $.firefly.random(($.firefly.eleWidth-50)),
opacity: $.firefly.opacity($.firefly.settings.twinkle)
}, (($.firefly.random(10) + 5) * 2000),function(){ $.firefly.fly(sp) } );
};
$.firefly.stop = function(sp) {
$(sp).stop();
};
$.firefly.randomPixel = function(min, max) {
return Math.floor(Math.random()*(max-min+1)+min);
}
$.firefly.random = function(max) {
return Math.ceil(Math.random() * max) - 1;
}
// set twinkle.
$.firefly.opacity = function(min)
{
op= Math.random();
if(op < min)
return 0;
else
return 1;
}
})(jQuery);