-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
jquery.shuffleLetters.js
executable file
·94 lines (67 loc) · 2.19 KB
/
jquery.shuffleLetters.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
// random shuffle
(function myshuffle($) {
$.fn.shuffleLetters = function (prop) {
var options = $.extend({
"step": 10,
"fps": 20,
"text": ""
}, prop)
return this.each(function () {
var el = $(this),
str = "";
el.data('animated', true);
if (options.text) {
str = options.text.split('');
}
else {
str = el.text().split('');
}
var types = [],
letters = [];
for (var i = 0; i < str.length; i++) {
var ch = str[i];
if (ch == " ") {
types[i] = "space";
continue;
}
else if (/[a-z]/.test(ch)) {
types[i] = "lowerLetter";
}
else if (/[A-Z]/.test(ch)) {
types[i] = "upperLetter";
}
else {
types[i] = "symbol";
}
letters.push(i);
}
el.html("");
(function shuffle(start) {
var i,
len = letters.length,
strCopy = str.slice(0);
if (start > len) {
el.data('animated', false);
return;
}
for (i = Math.max(start, 0); i < len; i++) {
if (i < start + options.step) {
strCopy[letters[i]] = randomChar(types[letters[i]]);
}
else {
strCopy[letters[i]] = "";
}
}
el.text(strCopy.join(""));
setTimeout(function () {
shuffle(start + 1);
}, 1000 / options.fps);
})(-options.step);
});
};
function randomChar(type) {
var pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,.?/\\(^)![]{}*&^%$#'\"";
var arr = pool.split('');
return arr[Math.floor(Math.random() * arr.length)];
}
})(jQuery);