-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.animati.js
235 lines (193 loc) · 6.65 KB
/
jquery.animati.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
/* ----------------------------------------------------------------------------------
$.animati plugin
Encoding: UTF-8
Version: v0.1
Authors:
Juan G. Hurtado [[email protected]]
----------------------------------------------------------------------------------
Table of contents
----------------------------------------------------------------------------------
1.DEFAULT CONFIG
2.LOCAL VARS
3.INIT METHOD
4.PUBLIC METHODS
4.1.move_to_next()
4.2.move_to_prev()
4.3.move_to($target)
5.PRIVATE METHODS
5.1.update_visual_state()
5.2.clear_timer()
6.INITIALIZATION
7.PLUGIN BINDING
---------------------------------------------------------------------------------- */
(function($) {
$.animati = function(element, options) {
/* =DEFAULT CONFIG
------------------------------------------------------------------------------ */
var defaults = {
autoStart: true,
animationTime: 500
}
/* =LOCAL VARS
------------------------------------------------------------------------------ */
var plugin = this,
animationsType = new Array('top','bottom','left','right'),
element = element,
$element = $(element),
$animati = $element,
$elements = $element.find('.animati-element'),
$animations = new Array();
plugin.settings = {};
plugin.animationTop = 'top';
plugin.animationBottom = 'bottom';
plugin.animationLeft = 'left';
plugin.animationRight = 'right';
/* =INIT METHOD
------------------------------------------------------------------------------ */
plugin.init = function() {
var ps = plugin.settings = $.extend({}, defaults, options);
var itemsEncontrados = 0;
if ($animati.hasClass('processed')) {
return;
}
// 1.- Leemos las posiciones originales de los elementos , el tipo de animacion, el orden y las guardamos
$elements
.each(function(index, animatiOrder){
var item = jQuery(animatiOrder);
//Lo oculto
item.css('visible', 'hidden');
//Posición
item.data('top', (item.position().top - $element.position().top));
item.data('bottom', ($element.outerHeight() - item.outerHeight() - item.data('top')));
item.data('left', (item.offset().left - $element.offset().left));
item.data('right', ($element.outerWidth() - item.outerWidth() - item.data('left') ));
item.data('width', item.width());
item.data('height', item.height());
//Tipo de animacion
var regExpType = new RegExp('animati-type-([a-zA-Z]+)');
var typeTemp = item.attr('class').match(regExpType);
if(typeTemp != null && animationsType.indexOf(typeTemp[1]) != -1){
item.data('type', typeTemp[1]);
}
//Orden de la animcion
var regExpOrder = new RegExp('animati-order-([0-9]+)');
var orderTemp = item.attr('class').match(regExpOrder);
if(orderTemp != null){
item.data('order', orderTemp[1]);
}
if(item.data('order') != undefined && item.data('type') != undefined){
itemsEncontrados++;
}
});
// 2. Creamos el array de animaciones con todos los elementos en orden
$animations = new Array(itemsEncontrados);
// 3.- Modificamos todos los elementos para posicionarlos absollutamente
$element.css({position:'relative', overflow:'hidden'});
plugin.reset();
// 4.- Lanzamos todas las animaciones en orden
if(ps.autoStart){
plugin.start();
}
// 5.- Mark as processed if everything was fine
$animati.addClass('processed');
}
/* =PUBLIC METHODS
------------------------------------------------------------------------------ */
plugin.reset = function(){
$elements
.each(function(index, animatiOrder){
var item = jQuery(animatiOrder);
var orden = item.data('order');
var tipo = item.data('type');
//Si no tiene definido order o tipo de animacion, pasamos
if(orden == undefined || tipo == undefined){
return;
}
//Lo recolocamos
item.css({position:'absolute', width:item.data('width')+"px", height:item.data('height')+"px", visibility: 'hidden'});
//En funcion del tipo de animacion, movemos las cosas donde sean
if(tipo == plugin.animationTop){
item.css('top', "-"+(item.data('height')+20)+"px");
item.css('left', item.data('left')+"px");
}else if(tipo == plugin.animationBottom){
item.css('bottom', "-"+(item.data('height')+20)+"px");
item.css('left', item.data('left')+"px");
}else if(tipo == plugin.animationLeft){
item.css('left', "-"+(item.data('width')+20)+"px");
item.css('top', item.data('top')+"px");
}else if(tipo == plugin.animationRight){
item.css('right', "-"+(item.data('width')+20)+"px");
item.css('top', item.data('top')+"px");
}else{
return;
}
//Guardamos la animacion donde corresponda
$animations[orden] = item;
});
}
plugin.start = function(){
plugin.animacionActual = 0;
plugin.makeNextAnimation();
}
plugin.makeNextAnimation = function(){
var $animacion = $animations[plugin.animacionActual];
if($animacion == null){
return;
}
var tipo = $animacion.data('type');
$animacion.css('visibility', 'visible');
if(tipo == plugin.animationTop){
$animacion
.animate(
{top : $animacion.data('top')+'px'},
ps.animationTime,
function(){
plugin.makeNextAnimation();
});
}else if(tipo == plugin.animationBottom){
$animacion
.css('visible', 'visible')
.animate(
{bottom : $animacion.data('bottom')+'px'},
ps.animationTime,
function(){
plugin.makeNextAnimation();
});
}else if(tipo == plugin.animationLeft){
$animacion
.css('visible', 'visible')
.animate(
{left : $animacion.data('left')+'px'},
ps.animationTime,
function(){
plugin.makeNextAnimation();
});
}else if(tipo == plugin.animationRight){
$animacion
.css('visible', 'visible')
.animate(
{right : $animacion.data('right')+'px'},
ps.animationTime,
function(){
plugin.makeNextAnimation();
});
}
plugin.animacionActual++;
}
/* =PRIVATE METHODS
------------------------------------------------------------------------------ */
/* =INITIALIZATION
------------------------------------------------------------------------------ */
plugin.init();
}
/* =PLUGIN BINDING
------------------------------------------------------------------------------ */
$.fn.animati = function(options) {
return this.each(function() {
if (undefined == $(this).data('animati')) {
var plugin = new $.animati(this, options);
$(this).data('animati', plugin);
}
});
}
})($);