-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextition.js
272 lines (220 loc) · 8.42 KB
/
textition.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
/* * * * * * * * * * * * * * * * * * * * * * *
* textition.js v1.0.2
* https://github.com/dimajt/textition.js
* Dima Karpov (c) 2013
* * * * * * * * * * * * * * * * * * * * * * */
(function($) {
$.fn.textition = function(options) {
// ----------------
// VARS
// ---------------
// options
var data = $.extend({
map: {
x: 100,
y: 50,
z: 1000
},
speed: 1,
animation: 'ease',
handler: 'click',
perspective: 300,
autoplay: false,
interval: 3,
element: this,
count: false
}, options);
// vars
var div = this,
child = this.children(),
letters,
index = 0,
position = {},
playing;
// ----------------
// UTILITY
// ---------------
// filter text
var filter = function(val) {
switch(val) {
case ' ':
return ' '
default:
return val
}
}
// get position
var getPosition = function() {
var span = child.first();
position.top = span.offset().top - div.offset().top;
position.left = span.offset().left - div.offset().left;
position.width = span.width();
}
// random distance
var val = function(val) {
return Math.round(Math.random() * val) - val / 2;
}
// ----------------
// BIND
// ---------------
// hide letters
var hide = function() {
var x = 0, y = 0, z = 0;
for (var i in data.map) {
switch(i) {
case 'x':
x = val(data.map.x)
break
case 'y':
y = val(data.map.y)
break
case 'z':
z = val(data.map.z)
break
}
}
$(this).css({
'-webkit-transform': 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)',
'-moz-transform': 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)',
'-ms-transform': 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)',
'-o-transform': 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)',
'transform': 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)'
});
}
// disappear
var disappear = function(span) {
$(span)
.css('opacity', 0)
.children()
.each(hide);
index++;
if (index > child.length - 1) {
index = 0;
}
}
// show
var show = function() {
$(this).css({
'-webkit-transform': 'translate3d(0px, 0px, 0px)',
'-moz-transform': 'translate3d(0px, 0px, 0px)',
'-ms-transform': 'translate3d(0px, 0px, 0px)',
'-o-transform': 'translate3d(0px, 0px, 0px)',
'transform': 'translate3d(0px, 0px, 0px)'
});
}
// appear
var appear = function(span) {
$(span)
.css('opacity', 1)
.children()
.each(show);
}
// run
var run = function(e) {
if (data.autoplay && e && !e.isTrigger) {
if (data.count != 0) {
clearInterval(playing)
playing = setInterval(play, data.interval * 1000);
}
}
disappear(child[index]);
appear(child[index]);
}
// ----------------
// CSS
// ---------------
// child style
var childStyle = function() {
$(this).css({
'-webkit-perspective': data.perspective + 'px',
'-moz-perspective': data.perspective + 'px',
'-ms-perspective': data.perspective + 'px',
'-o-perspective': data.perspective + 'px',
'perspective': data.perspective + 'px',
'-webkit-transform-style': 'preserve-3d',
'-moz-transform-style': 'preserve-3d',
'-ms-transform-style': 'preserve-3d',
'-o-transform-style': 'preserve-3d',
'transform-style': 'preserve-3d',
'-webkit-transition': 'opacity ' + data.speed + 's ' + data.animation,
'-moz-transition': 'opacity ' + data.speed + 's ' + data.animation,
'-ms-transition': 'opacity ' + data.speed + 's ' + data.animation,
'-o-transition': 'opacity ' + data.speed + 's ' + data.animation,
'transition': 'opacity ' + data.speed + 's ' + data.animation,
'position': 'absolute',
'display': 'block',
'width': position.width,
'left': position.left,
'top': position.top
});
}
// div style
var divStyle = function() {
var pos = div.css('position');
div.css({
'position': (pos === 'static') ? 'relative' : pos,
'height': div.height(),
'width': div.width()
});
}
// letter style
var letterStyle = function() {
$(this).css({
'-webkit-transition': '-webkit-transform ' + data.speed + 's ' + data.animation,
'-moz-transition': '-moz-transform ' + data.speed + 's ' + data.animation,
'-ms-transition': '-ms-transform ' + data.speed + 's ' + data.animation,
'-o-transition': '-o-transform ' + data.speed + 's ' + data.animation,
'transition': 'transform ' + data.speed + 's ' + data.animation,
'display': 'inline-block'
})
}
// ----------------
// INITIALIZATION
// ---------------
// create letters
var createLetters = function() {
var span = $(this);
var text = span.text();
var code = '';
for (var i = 0; i < text.length; i++) {
code += '<l>' + filter(text[i]) + '</l>';
}
span.html(code);
}
// draw
var draw = function() {
var other = child.not(':first');
other.hide().css('opacity', 0);
divStyle();
child.each(createLetters);
child.each(childStyle);
letters = div.find('l');
letters.each(letterStyle);
other.show().children().each(hide);
}
// play
var play = function() {
run();
if (data.count) {
data.count--;
if (data.count == 0) {
clearInterval(playing);
}
}
}
// init
var init = function() {
var time = new Date();
getPosition();
draw();
if (data.handler) {
data.element.bind(data.handler, run);
}
if (data.autoplay) {
playing = setInterval(play, data.interval * 1000);
}
}
// Run
init();
}
})(jQuery)