This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
/
jquery.turniquet.js
183 lines (160 loc) · 6.53 KB
/
jquery.turniquet.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
(function($) {
$.fn.turniquet = function(options) {
// This is the easiest way to have default options.
var settings = $.extend({
// These are the defaults.
zindex : 500,
direction: 'up',
reverseZindex: false,
duration: 200,
easing: 'swing',
animateOnCLick: true,
triggerUp: null,
triggerDown: null,
complete: null,
before: null,
after: null,
loop: true,
validate: function(){return true;}
}, options);
var windowWidth = this.parent().width(),
windowHeight = this.parent().height(),
zIndexI = settings.zindex,
children = this.children(),
nbChild = children.length,
childWidth = $(children[0]).width(),
childHeight = $(children[0]).height(),
positions = [],
objectMovingCounter = 0,
animationCounter = 0;
var animate = function(direction){
if(typeof direction != 'string'){
direction = settings.direction;
}
// check if no animation is running and if the current slide is allow to change
if(objectMovingCounter == 0 && settings.validate(animationCounter)){
// stop looping
if(settings.loop == false && ((animationCounter+1) >= nbChild)){
return false;
}
// for each child of main collection
children.each(function(index, element){
var $c = $(element),
dataIndex = parseInt($c.attr('data-index')),
newIndex = 0,
down = null,
animTop = '',
animLeft = '';
if(direction == 'up'){// up
newIndex = nbChild-1;
down = true;
if(dataIndex > 0){
newIndex = dataIndex-1;
down = false;
}
}else{ // down
newIndex = 0;
down = false;
if(dataIndex < (nbChild-1)){
newIndex = dataIndex+1;
down = true;
}
}
// Change data index
$c.attr('data-index', newIndex);
// Trigger before method
if(typeof settings.before == 'function'){
settings.before(index, element);
}
if(!down){
animTop = '-='+($c.position().top - positions[newIndex].top);
animLeft = '-='+($c.position().left - positions[newIndex].left);
}else{
if(direction == 'up'){
animTop = '+='+($c.position().top + positions[newIndex].top);
animLeft = '+='+($c.position().left + positions[newIndex].left);
}else{
animTop = '+='+(positions[newIndex].top - $c.position().top);
animLeft = '+='+(positions[newIndex].left - $c.position().left);
}
}
objectMovingCounter++;
// change z-index
if(direction == 'up'){
$c.css('z-index', positions[newIndex].zindex);
}else{// delay z-index
var ozIndex = {
zindex : positions[newIndex].zindex
};
setTimeout(function(){
$c.css('z-index', ozIndex.zindex);
}, settings.duration);
}
// Animate slides
$c.animate({
left: animLeft,
top: animTop
}, settings.duration, settings.easing, function(){
if(objectMovingCounter > 0){
objectMovingCounter --;
}
});
// Trigger before method
if(typeof settings.before == 'function'){
setTimeout(function(){
settings.after(index, element);
}, settings.duration);
}
});
// Trigger complete method
if(typeof settings.complete == 'function'){
setTimeout(settings.complete, settings.duration);
}
animationCounter++;
}
};
// Initilize First Position
children.each(function(index, element){
var $e = $(element);
if(settings.reverseZindex == false){
zIndexI--;
}else{
zIndexI++;
}
// Calculate position of each slide
var left = (((windowWidth-(childWidth))/(nbChild-1))*index);
var top = (((windowHeight-(childHeight))/(nbChild-1))*index);
positions[index] = {
'top':top,
'left':left,
'zindex':zIndexI
};
// Position slides
$e.css('top', top)
.css('left', left)
.css('z-index', zIndexI)
.attr('data-index', index);
});
//Manage Click
if(settings.animateOnCLick){
children.click(animate);
}
if(settings.triggerUp != null){
var $triggerUp = $(settings.triggerUp);
if($triggerUp.length > 0){
$triggerUp.click(function(){
animate('up');
});
}
}
if(settings.triggerDown != null){
var $triggerDown = $(settings.triggerDown);
if($triggerDown.length > 0){
$triggerDown.click(function(){
animate('down');
});
}
}
return this;
};
}(jQuery));