-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.typographytroll.js
51 lines (49 loc) · 1.42 KB
/
jquery.typographytroll.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
/**
* TypographyTroll
*
* A jQuery plugin for confusing typography geeks
* https://github.com/mcoconnor/jquery-TypographyTroll
*
* © 2011 Michael O'Connor
* [michael at mcoconnor dot net]
* <http://www.mcoconnor.net>
*
* For usage and examples, visit:
* http://mcoconnor.github.com/jquery-typographyTroll/
*
* Licensed under the MIT:
* http://www.opensource.org/licenses/mit-license.php
*
*/
(function( $ ) {
$.fn.typographyTroll = function(fontFamilies,fallback) {
var ret = [];
//get the text nodes
this.contents().each( function() {
var fn = arguments.callee;
if (this.nodeType == 3)
ret.push(this);
else $(this).contents().each(fn);
});
//trolololol
if (fontFamilies === undefined) fontFamilies = ['Helvetica','Arial'];
if (fallback === undefined) fallback = 'sans-serif';
var spans = [];
var temp;
for (i in fontFamilies){
spans.push("</span><span style='font-family:"+fontFamilies.join(', ')+', '+fallback+";'>");
temp = fontFamilies.shift();
fontFamilies.push(temp);
}
$(ret).replaceWith(function(){
var pieces = [spans[0].substr(7)];
var chars = this.data.split("");
for (i in chars){
pieces.push(chars[i]);
if (!/\s/.test(chars[i]) && i != pieces.length-1) pieces.push(spans[Math.floor(Math.random()*spans.length)]);
}
pieces.push('</span>');
return pieces.join("");
});
}
})( jQuery );