forked from davatron5000/Lettering.js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lettering.js
76 lines (66 loc) · 1.87 KB
/
lettering.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
/* Pure javascript version of lettering.js without dependencies */
/*!
* Lettering.JS 0.6.1
*
* Copyright 2010, Dave Rupert http://daverupert.com
* Released under the WTFPL license
* http://sam.zoy.org/wtfpl/
*
* Thanks to Paul Irish - http://paulirish.com - for the feedback.
*
* Date: Mon Sep 20 17:14:00 2010 -0600
*/
(function(){
function injector(t, splitter, klass, after) {
//Find a cross browser innerText
if (typeof t.textContent != 'undefined') {
var a = t.textContent;
} else {
var a = t.innerText;
}
var a = a.split(splitter), inject = '';
if (a.length) {
for(var i=0, len=a.length; item=a[i], i<len; i++) {
inject += '<span class="'+klass+(i+1)+'">'+item+'</span>'+after;
}
t.innerHTML = inject;
}
}
var methods = {
init : function( el ) {
for(var i=el.length-1; i>=0; --i) {
injector(el[i], '', 'char', '');
}
},
words : function( el ) {
for(var i=el.length-1; i>=0; --i) {
injector(el[i], ' ', 'word', ' ');
}
},
lines : function( el ) {
for(var i=el.length-1; i>=0; --i) {
var r = "eefec303079ad17405c889e092e105b0";
// Because it's hard to split a <br/> tag consistently across browsers,
// (*ahem* IE *ahem*), we replaces all <br/> instances with an md5 hash
// (of the word "split"). If you're trying to use this plugin on that
// md5 hash string, it will fail because you're being ridiculous.
el[i].innerHTML = el[i].innerHTML.replace(/<br\s*[\/]?>/gi, r);
injector(el[i], r, 'line', '');
}
}
};
lettering = function( el, method ) {
if (el == null) {
return;
}
if (typeof(el.length) == "undefined") {
el = [el];
}
// Method calling logic
if ( method && methods[method] ) {
methods[ method ](el);
} else if ( method === 'letters' || ! method ) {
methods.init(el);
}
};
})();