This repository has been archived by the owner on Aug 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
rainbow.js
108 lines (75 loc) · 2.04 KB
/
rainbow.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
(function($) {
$.fn.rainbow = function(options) {
this.each(function() {
options.originalText = $(this).html();
options.iterations = 0;
if (!options.pauseLength) {
options.pauseLength = options.animateInterval;
}
$(this).data('options',options);
if (options.pad) {
for (x = 0; x < options.originalText.length; x++) {
options.colors.unshift(options.colors[options.colors.length-1]);
}
}
$.fn.rainbow.render(this);
});
}
$.fn.pauseRainbow = function() {
this.each(function() {
var options = $(this).data('options');
if (options) {
options.animate = false;
$(this).data('options',options);
}
});
}
$.fn.resumeRainbow = function() {
this.each(function() {
var options = $(this).data('options');
if (options) {
options.animate = true;
$(this).data('options',options);
$.fn.rainbow.render(this);
}
});
}
$.fn.rainbow.render = function(obj) {
var options = $(obj).data('options');
var chars = options.originalText.split('');
options.iterations++;
var newstr = '';
var counter = 0;
for (var x in chars) {
if (chars[x]!=' ') {
newstr = newstr + '<span style="color: ' + options.colors[counter] + ';">' + chars[x] + '</span>';
counter++;
} else {
newstr = newstr + ' ';
}
if (counter >= options.colors.length) {
counter = 0;
}
}
$(obj).html(newstr);
var pause = (options.iterations % options.colors.length == 0);
if (options.animate) {
(
function(obj,interval) {
var options = $(obj).data('options');
var i = setTimeout(function() {
$.fn.rainbow.shift(obj);
},interval);
options.interval = i;
$(obj).data('options',options);
}
)(obj,pause?options.pauseLength:options.animateInterval);
}
}
$.fn.rainbow.shift = function(obj) {
var options = $(obj).data('options');
var color = options.colors.pop();
options.colors.unshift(color);
$.fn.rainbow.render(obj);
}
})(jQuery);