-
Notifications
You must be signed in to change notification settings - Fork 40
/
jquery-letterfx.js
332 lines (230 loc) · 8.88 KB
/
jquery-letterfx.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
(function ($) {
"use strict";
var LetterFx = function (element, options) {
this.options = $.extend({}, $.fn.letterfx.defaults, options);
this.num_completed_fx = 0;
this.is_done = false;
this.monitor_timer = null;
this.killswitch=null;
this.$element = $(element);
if(this.options.restore)
this.original_html = this.$element.html();
this.init();
}
LetterFx.prototype.init = function(){
this.new_html = this.$element.text().replace( this.options.pattern, this.options.replacement );
this.$element.addClass(this.options.css.element.base).addClass( this.options.css.element.before );
this.$element.html( this.new_html );
this.$letters = this.$element.find(this.options.selector);
this.$letters
.css('transition-duration', this.options.fx_duration)
.addClass(this.options.css.letters.base)
.addClass(this.options.css.letters.before);
this.bindLetterFxEnd();
this.num_letters = this.$letters.length;
this.fx();
return this;
}
LetterFx.prototype.bindLetterFxEnd = function(){
var options = this.options;
var lfx = this;
this.$letters.bind("transitionend", function(){
options.onLetterComplete( $(this), lfx.$element, lfx );
lfx.notifyFXEnd( );
switch(options.letter_end){
case "destroy":
$(this).remove();
break;
case "rewind":
lfx.applyLetterFx( $(this), options.timing, options.css.letters.after, options.css.letters.before );
break;
case "stay":
break;
// restore
default:
$(this).replaceWith( $(this).text() );
}
});
return lfx;
}
LetterFx.prototype.terminate=function(){
this.is_done = true;
this.options.onElementComplete(this.$element, this);
clearTimeout(this.killswitch);
switch(this.options.element_end){
case "destroy":
this.$element.remove();
break;
case "stay":
break;
// restore
default:
this.$element.html( this.original_html );
this.$element.removeClass( this.options.css.element.base ).removeClass(this.options.css.element.after);
break;
}
}
LetterFx.prototype.notifyFXEnd=function(){
clearTimeout(this.monitor_timer);
this.num_completed_fx++;
var lfx = this;
this.monitor_timer = setTimeout(
function(){
if(lfx.num_completed_fx % lfx.num_letters===0){
lfx.terminate();
}
},
Math.max(this.options.timing+10, 50)
);
return this;
}
LetterFx.prototype.startKillWatch = function(){
var fx_duration = this.options.fx_duration.match(/\d+s/) ? parseInt(this.options.fx_duration) : 1;
var time = Math.ceil(1.5 * this.num_letters * this.options.timing * fx_duration );
var lfx = this;
this.killswitch = window.setTimeout(function(){
if(!lfx.isDone()){
lfx.terminate()
}
}, time)
}
LetterFx.prototype.fx = function(){
var lfx = this;
this.startKillWatch();
this.$element.removeClass(this.options.css.element.before).addClass(this.options.css.element.after);
var $letters = this.options.sort(this.$letters);
var options = this.options;
$letters.each(
function(i, letter){
lfx.applyLetterFx( $(letter), (i+1)*options.timing, options.css.letters.before, options.css.letters.after);
}
);
return this;
}
LetterFx.prototype.applyLetterFx = function($letter, timing, css_before, css_after){
var options = this.options;
window.setTimeout(
function(){
$letter.removeClass(css_before).addClass(css_after);
},
timing
);
return this
}
LetterFx.prototype.isDone = function(){
return this.is_done;
}
var LetterFxConfig = function(conf){
this.config = $.extend({}, $.fn.letterfx.defaults, conf);
this.buildCss(this.config.backwards);
// check & change to word pattern
if(this.config.words)
this.config.pattern=/(\S+)/g;
}
LetterFxConfig.prototype.buildCss = function(flip){
var options = this.config;
var before = flip ? 'after' : 'before';
var after = flip ? 'before' : 'after';
var css = { element:{}, letters:{} };
css.element.base=options.element_class+ "-container " + options.fx.replace(/(\S+)/g, options.element_class + "-$1-container");
css.element[before]=options.fx.replace(/(\S+)/g, options.element_class + "-$1-before-container");
css.element[after]=options.fx.replace(/(\S+)/g, options.element_class + "-$1-after-container");
css.letters.base = options.element_class;
css.letters[before] = options.fx.replace(/(\S+)/g, options.element_class + "-$1-before")
css.letters[after] = options.fx.replace(/(\S+)/g, options.element_class + "-$1-after")
this.config = $.extend( options, {'css':css} );
}
LetterFxConfig.prototype.getConfig = function(){
return this.config;
}
LetterFxConfig.parse=function(config){
return (new LetterFxConfig( config )).getConfig();
}
$.fn.letterfx=function(config){
config = LetterFxConfig.parse( config );
return $(this).each(function(){
var $element = $(this);
if (! $element.data('letterfx-obj') || $element.data('letterfx-obj').isDone() ){
$element.data('letterfx-obj', new LetterFx( $element, config ) );
}
});
};
$.fn.letterfx.sort={
random:function(array){
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
},
reverse:function($array){
return $array.toArray().reverse();
}
}
$.fn.letterfx.patterns={
letters:/(\S)/gi
}
// Plugin Configurables
$.fn.letterfx.defaults = {
// Default fx
fx:'spin fly-top',
// defaults to selecting all characters
pattern:/(\S)/gi,
// switch from letter FX to word FX.
word:false,
// fx, like fly or fade, can happen in (eg fade-in) or out (eg fade-out).
// The default is in; change backwards to true to reverse the order of the effects.
backwards:false,
// defaults to injecting spans, can be any inline element
replacement:"<span>$1</span>",
//selector -- should match replacement above
selector:'span',
// letter fx start sequentially: letters start their fx one after another.
// this sets time between the letters
timing:50,
//duration of each fx
// options the same as css property for transition-duration ("1ms", "1s", etc)
fx_duration:"1s",
// stabile dimensions
// stabilize:true,
// sort callback for custom sorting elements
sort:function($letters){ return $letters; },
// Callback when letter is done animating. Runs after each letter
onLetterComplete:function($letter, $element, LetterFXObj){},
// Runs after all are done.
onElementComplete:function($element, LetterFXObj){},
// what to do when a letter completes its animation.
// options include
// restore: return letter to plain text (default)
// destroy: get rid of the letter.
// stay: leave it as is.
// rewind: reverse the animation
letter_end:"restore",
// what to do when the entire element has completed all its letter effects
// options include:
// restore: return element to its pre-fx state (default)
// stay: do nothing
// destroy: get rid of the element... FOREVER
element_end:"restore",
// Restore container element back to original state.
// options: true, false, "element" ("element" waits until all letters complete fx before restoring)
restore:true,
// destroy element/letters after fx, useful on {out:true} fx
// options: true, false, "letters" ("letters" destroys each letter after fx, but elements original content may be restored after all letters complete fx)
destroy:false,
// default class for injected elements
element_class:'letterfx',
// placeholder values that are calculated on launch
css:{
element:{ base:'', before:'', after:''},
letters:{ base:'', before:'', after:''}
}
};
}(jQuery));