forked from jschr/textillate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.textillate.js
220 lines (174 loc) · 5.09 KB
/
jquery.textillate.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
/*!
* texillate.js
* http://jschr.github.com/textillate
* MIT licensed
*
* Copyright (C) 2012-2013 Jordan Schroter
*/
(function ($) {
"use strict";
function isInEffect (effect) {
return /In/.test(effect) || $.inArray(effect, $.fn.textillate.defaults.inEffects) >= 0;
};
function isOutEffect (effect) {
return /Out/.test(effect) || $.inArray(effect, $.fn.textillate.defaults.outEffects) >= 0;
};
// custom get data api method
function getData (node) {
var attrs = node.attributes || []
, data = {};
if (!attrs.length) return data;
$.each(attrs, function (i, attr) {
if (/^data-in-*/.test(attr.nodeName)) {
data.in = data.in || {};
data.in[attr.nodeName.replace(/data-in-/, '')] = attr.nodeValue;
} else if (/^data-out-*/.test(attr.nodeName)) {
data.out = data.out || {};
data.out[attr.nodeName.replace(/data-out-/, '')] = attr.nodeValue;
} else if (/^data-*/.test(attr.nodeName)) {
data[attr.nodeName] = attr.nodeValue;
}
})
return data;
}
function shuffle (o) {
for (var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
function animate ($c, effect, cb) {
$c.addClass('animated ' + effect)
.css('visibility', 'visible')
.show();
$c.one('animationend webkitAnimationEnd oAnimationEnd', function () {
$c.removeClass('animated ' + effect);
cb && cb();
});
}
function animateChars ($chars, options, cb) {
var that = this
, count = $chars.length;
if (!count) {
cb && cb();
return;
}
if (options.shuffle) shuffle($chars);
$chars.each(function (i) {
var $this = $(this);
function complete () {
if (isInEffect(options.effect)) {
$this.css('visibility', 'visible');
} else if (isOutEffect(options.effect)) {
$this.css('visibility', 'hidden');
}
count -= 1;
if (!count && cb) cb();
}
var delay = options.sync ? options.delay : options.delay * i * options.delayScale;
$this.text() ?
setTimeout(function () { animate($this, options.effect, complete) }, delay) :
complete();
})
};
var Textillate = function (element, options) {
var base = this
, $element = $(element);
base.init = function () {
base.$texts = $element.find(options.selector);
if (!base.$texts.length) {
base.$texts = $('<ul class="texts"><li>' + $element.text() + '</li></ul>');
$element.html(base.$texts);
}
base.$texts.hide();
base.$current = $('<span>')
.text(base.$texts.find(':first-child').text())
.prependTo($element);
base.setOptions(options);
setTimeout(function () {
base.options.autoStart && base.start();
}, base.options.initialDelay)
};
base.setOptions = function (options) {
base.options = options;
};
base.start = function (index) {
var $next = base.$texts.find(':nth-child(' + (index || 1) + ')');
(function run ($elem) {
var options = $.extend({}, base.options, getData($elem));
base.$current
.text($elem.text())
.lettering('words');
base.$current.find('[class^="word"]')
.css({
'display': 'inline-block',
// fix for poor ios performance
'-webkit-transform': 'translate3d(0,0,0)',
'-moz-transform': 'translate3d(0,0,0)',
'-o-transform': 'translate3d(0,0,0)',
'transform': 'translate3d(0,0,0)'
})
.each(function () { $(this).lettering() });
var $chars = base.$current.find('[class^="char"]')
.css('display', 'inline-block');
if (isInEffect(options.in.effect)) {
$chars.css('visibility', 'hidden');
} else if (isOutEffect(options.in.effect)) {
$chars.css('visibility', 'visible');
}
animateChars($chars, options.in, function () {
setTimeout(function () {
// in case options have changed
var options = $.extend({}, base.options, getData($elem));
var $next = $elem.next();
if (base.options.loop && !$next.length) {
$next = base.$texts.find(':first-child');
}
if (!$next.length) return;
animateChars($chars, options.out, function () {
run($next)
});
}, base.options.minDisplayTime);
});
}($next));
};
base.destroy = function () {
};
base.init();
}
$.fn.textillate = function (settings, args) {
return this.each(function () {
var $this = $(this)
, data = $this.data('textillate')
, options = $.extend(true, {}, $.fn.textillate.defaults, getData(this), typeof settings == 'object' && settings);
if (!data) {
$this.data('textillate', (data = new Textillate(this, options)));
} else if (typeof settings == 'string') {
data[settings].apply(data, [].concat(args));
} else {
data.setOptions.call(data, options);
}
})
};
$.fn.textillate.defaults = {
selector: '.texts',
loop: false,
minDisplayTime: 2000,
initialDelay: 0,
in: {
effect: 'fadeInLeftBig',
delayScale: 1.5,
delay: 50,
sync: false,
shuffle: false
},
out: {
effect: 'hinge',
delayScale: 1.5,
delay: 50,
sync: false,
shuffle: false,
},
autoStart: true,
inEffects: [],
outEffects: [ 'hinge' ]
};
}(jQuery));