-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathjquery.funnyText.js
297 lines (247 loc) · 8.22 KB
/
jquery.funnyText.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
/**
* funnyText.js 0.4 Beta
* https://github.com/alvarotrigo/funnyText.js
* MIT licensed
*
* Copyright (C) 2013 alvarotrigo.com - A project by Alvaro Trigo
*/
(function($){
$.fn.funnyText = function(options){
// Create some defaults, extending them with any options that were provided
options = $.extend({
'speed': 700,
'borderColor': 'black',
'activeColor': 'white',
'color': 'black',
'fontSize': '7em',
'direction': 'both'
}, options);
var that = $(this);
that.addClass('funnyText');
var original = $(this);
var characters = $.trim(original.text()).split('');
var positionsY = ['top', 'bottom'];
var positionsX = ['left', 'right'];
var activePositionX, activePositionY, normalPositionX, normalPositionY;
var previousPosition;
//removing the original text
original.html('');
//append the CSS styles
var style = $('<style>'+that.selector +'.funnyText span.active { color: ' + options.activeColor + '; text-shadow: -1px 0 '+options.borderColor+', 0 1px '+options.borderColor+', 1px 0 '+options.borderColor+', 0 -1px '+options.borderColor+';} '+that.selector +'.funnyText span{color: ' + options.color +'; font-size:' + options.fontSize + ';}</style>')
$('html > head').append(style);
//for each character
for (var i = 0; i < characters.length; i++){
normalPositionY = positionsY[getRandom(0, 100) % 2];
normalPositionX = positionsX[getRandom(0, 100) % 2];
if(characters[i] == ' '){
characters[i] = ' ';
}
var visibleChar = '<span class="normal ' + normalPositionY + ' ' + normalPositionX + '">' + characters[i] + '</span>';
//avoid repeating the same values two consecutive letters
do{
activePositionXY = getNewPosition(normalPositionX, normalPositionY);
}while(activePositionXY == previousPosition && options.direction == 'both');
previousPosition = activePositionXY;
var activeChar = '<span class="active ' + activePositionXY + '">' + characters[i] + '</span>';
var newChar = '<div class="charWrap">' + visibleChar + activeChar + '</div>';
original.append(newChar);
};
//setting the width and height of each character to its wrapper
that.find('.charWrap').each(function (){
var sizeX = $(this).find('span').outerWidth();
var sizeY = $(this).find('span').outerHeight();
$(this).css({
'width': sizeX * 2 + 'px',
'height': sizeY * 2 + 'px'
});
//adjusting the wrappers positions
setMargin($(this));
//creating the "viewport" for each character.
$(this).wrap('<div class="character" style="width:' + sizeX + 'px; height: ' + sizeY + 'px"></div>');
});
/**
* Returnsn a random number between two values.
*/
function getRandom(from, to){
return from + Math.floor(Math.random() * (to +1));
}
/**
* Gets a new position for the active character in a way it can be scrolled
* vertically or horizontally from the position of the original character.
* (from top left to botom right wouldn't work, for example)
*/
function getNewPosition(x, y){
var result;
if((getRandom(0, 100) % 2 && options.direction == 'both') || options.direction == 'horizontal') {
if(x == "right" && y == "top"){
result = "left top moveLeft";
} else if(x == "right" && y == "bottom"){
result = "left bottom moveLeft";
} else if(x == "left" && y == "top"){
result = "right top moveRight";
} else if(x == "left" && y == "bottom"){
result = "right bottom moveRight";
}
}else{
if(x == "right" && y == "top"){
result = "right bottom moveDown";
} else if(x == "right" && y == "bottom"){
result = "right top moveUp";
} else if(x == "left" && y == "top"){
result = "left bottom moveDown";
} else if(x == "left" && y == "bottom"){
result = "left top moveUp";
}
}
return result;
}
/**
* Sets the margin for the characters container depending on the position
* of the characters to show.
*/
function setMargin(obj){
if(obj.find('.normal').hasClass('bottom')){
obj.css('top', '-' + obj.find('.normal').outerHeight() + 'px');
}else{
obj.css('top', '0px');
}
if(obj.find('.normal').hasClass('right')){
obj.css('left', '-' + obj.find('.normal').outerWidth() + 'px');
}else{
obj.css('left', '0px');
}
}
//random movement for the characters of the title
setInterval(function (){
var randomTime = getRandom(2, 6);
var previousNum = '';
do{
var num = getRandom(0, characters.length - 1);
}while(num === previousNum);
previousNum = num;
setTimer(that.find('.charWrap').eq(num), randomTime);
}, 1 * options.speed);
/**
* Sets a timer for a given character for a given time.
*/
function setTimer(character, time){
setTimeout(function (){
moveCharacter(character);
}, time * options.speed);
}
$('.charWrap').hover(function (){
if(!$(this).hasClass('moved')){
moveCharacter($(this));
}
});
/**
* Moves a character to the destination position.
* Once reached, it will add a class "moved" as an status indicator.
*/
function moveCharacter(characterWrap){
var sizeY = characterWrap.outerHeight() / 2;
var sizeX = characterWrap.outerWidth() / 2;
var character = characterWrap.find('.active');
if(supportTransitions()){
if(character.hasClass('moveRight')){
if(!characterWrap.hasClass('moved')){
characterWrap.css('left', '-' + sizeX + 'px');
}else{
characterWrap.css('left', '0px');
}
} else if(character.hasClass('moveLeft')){
if(!characterWrap.hasClass('moved')){
characterWrap.css('left', '0px');
}else{
characterWrap.css('left', '-' + sizeX + 'px');
}
} else if(character.hasClass('moveUp')){
if(!characterWrap.hasClass('moved')){
characterWrap.css('top', '0px');
}else{
characterWrap.css('top', '-' + sizeY + 'px');
}
} else if(character.hasClass('moveDown')){
if(!characterWrap.hasClass('moved')){
characterWrap.css('top', '-' + sizeY + 'px');
}else{
characterWrap.css('top', '0px');
}
}
}
//jquery fallback
else{
if(character.hasClass('moveRight')){
if(!characterWrap.hasClass('moved')){
characterWrap.animate({
'left': '-' + sizeX + 'px'
}, 400);
}else{
characterWrap.animate({
'left': '0px'
},400);
}
} else if(character.hasClass('moveLeft')){
if(!characterWrap.hasClass('moved')){
characterWrap.animate({
'left': '0px'
},400);
}else{
characterWrap.animate({
'left' : '-' + sizeX + 'px'
},400);
}
} else if(character.hasClass('moveUp')){
if(!characterWrap.hasClass('moved')){
characterWrap.animate({
'top': '0px'
}, 400);
}else{
characterWrap.animate({
'top': '-' + sizeY + 'px'
}, 400);
}
} else if(character.hasClass('moveDown')){
if(!characterWrap.hasClass('moved')){
characterWrap.animate({
'bottom' : sizeY + 'px'
}, 400);
}else{
characterWrap.animate({
'bottom':'0px'
},400);
}
}
}
characterWrap.toggleClass('moved');
}
};
/**
* jQuery.support.cssProperty
* To verify that a CSS property is supported (or any of its browser-specific implementations)
*
*
* @Author: Axel Jack Fuchs (Cologne, Germany)
* @Date: 08-29-2010 18:43
*
* Example: $.support.cssProperty('boxShadow');
* Returns: true
*
* Example: $.support.cssProperty('boxShadow', true);
* Returns: 'MozBoxShadow' (On Firefox4 beta4)
* Returns: 'WebkitBoxShadow' (On Safari 5)
*/
function supportTransitions() {
var b = document.body || document.documentElement;
var s = b.style;
var p = 'transition';
if(typeof s[p] == 'string') {return true; }
// Tests for vendor specific prop
v = ['Moz', 'Webkit', 'Khtml', 'O', 'ms', 'Icab'],
p = p.charAt(0).toUpperCase() + p.substr(1);
for(var i=0; i<v.length; i++) {
if(typeof s[v[i] + p] == 'string') { return true; }
}
return false;
}
})(jQuery);