-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcarousel.js
More file actions
243 lines (201 loc) · 6.8 KB
/
carousel.js
File metadata and controls
243 lines (201 loc) · 6.8 KB
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
;(function($) {
"use strict";
var SashnCarousel = function(element, options) {
this.init(element, options);
};
SashnCarousel.DEFAULTS = {
'containerSelector': '.js-carousel-container',
'itemSelector': '.js-carousel-item',
'btnLeftSelector': '.js-carousel-btn-left',
'btnRightSelector': '.js-carousel-btn-right',
'visibleItemScale': [0.5, 0.7, 1.0, 0.7, 0.5],
'animationStepCount': 100,
'useAnimation': true,
//the following options are being calculated
'animatedItemScale': null,
'animatedItemCount': null,
'visibleItemCount': null,
'itemDefaultWidth': null,
'itemDefaultHeight': null,
'centerItemIndex': null,
'defaultCenterItemIndex': null
};
//SPOE =)
SashnCarousel.prototype.init = function(element, options) {
this.$element = $(element);
this.options = $.extend({}, SashnCarousel.DEFAULTS, options);
this.isCurrentlyBeingAnimated = false;
this.$items = this.$element.find(this.options.itemSelector);
this.visibleItemIndices = [];
this.currentSubPosition = 1;
this.distanceToAnimate = 0;
this.myTimeout = null;
this.$itemsToAnimate = [];
this.isCurrentlyBeingAnimated = false;
this.calculateOptions();
this.adjustItemPositions();
this.initControls();
};
SashnCarousel.prototype.getOptions = function() {
window.debugvar = this.options;
};
SashnCarousel.prototype.calculateOptions = function() {
var o = this.options;
//add one item before and after the visible items. those are needed for fade in/fade out of the outer items during animation
o.animatedItemScale = $.merge($.merge([0], o.visibleItemScale), [0]);
o.animatedItemCount = o.animatedItemScale.length;
o.visibleItemCount = o.visibleItemScale.length;
o.itemDefaultWidth = this.$items.eq(0).width();
o.itemDefaultHeight = this.$items.eq(0).height();
o.defaultCenterItemIndex = Math.floor(o.visibleItemCount/2);
if(!o.centerItemIndex) {
o.centerItemIndex = o.defaultCenterItemIndex;
}
};
SashnCarousel.prototype.getPositionCss = function(pos, step) {
var o = this.options
, step = step || 0
, factor = o.animatedItemScale[pos] + (pos == o.animatedItemCount-1 ? 0 : (o.animatedItemScale[pos+1] - o.animatedItemScale[pos]) * step/o.animationStepCount)
, width = o.itemDefaultWidth * factor
, height = o.itemDefaultHeight * factor
, getLeftCss = function(pos, step) {
var left = 0;
for (var i = 0; i <= pos; i++) {
var itemWidth = o.itemDefaultWidth * o.animatedItemScale[i];
if (i == pos) {
itemWidth *= step;
}
left += itemWidth;
};
return left;
};
return {
'width': width,
'height': height,
'top': (o.itemDefaultHeight - height) / 2,
'left': getLeftCss(pos, step/o.animationStepCount),
'z-index': factor * 100
};
};
SashnCarousel.prototype.adjustItemPositions = function() {
this.determineVisibleItemIndices();
this.$items.hide();
for (var i = 0; i < this.options.visibleItemCount; i++) {
//this.getPositionCss(i+1) <-- the +1 is needed, because this.getPositionCss already returns the css for the fade in/fade out (outer) item, which needs to be skipped in this situation
this.$items.eq(this.visibleItemIndices[i]).css(this.getPositionCss(i+1)).show();
};
};
SashnCarousel.prototype.determineVisibleItemIndices = function() {
var o = this.options;
this.visibleItemIndices = [];
for (var i = 0; i < o.visibleItemCount; i++) {
var index = this.adjustIndex(o.centerItemIndex + i - o.defaultCenterItemIndex);
this.visibleItemIndices.push(index);
};
};
SashnCarousel.prototype.adjustIndex = function(index) {
var itemCount = this.$items.length;
if (index < 0) {
index += itemCount;
} else if (index >= itemCount) {
index -= itemCount;
}
return index;
};
SashnCarousel.prototype.initControls = function() {
var o = this.options
, self = this
, $btnLeft = this.$element.find(o.btnLeftSelector)
, $btnRight = this.$element.find(o.btnRightSelector);
$btnLeft.click(function() {
self.distanceToAnimate -= 1;
if (!self.isCurrentlyBeingAnimated) {
self.animationLoop('left');
}
});
$btnRight.click(function() {
self.distanceToAnimate += 1;
if (!self.isCurrentlyBeingAnimated) {
self.animationLoop('right');
}
});
};
SashnCarousel.prototype.animationLoop = function(direction) {
var o = this.options;
if (!this.isCurrentlyBeingAnimated) {
//start animation
var directionModifier = direction == 'left' ? 0 : 1
, $farLeftItem = this.$items.eq(this.adjustIndex(o.centerItemIndex - Math.floor(o.animatedItemCount/2)))
, $farRightItem = this.$items.eq(this.adjustIndex(o.centerItemIndex + Math.floor(o.animatedItemCount/2)));
this.isCurrentlyBeingAnimated = true;
//show and hide items on the far left and right side
if (direction == 'left') {
$farLeftItem.show();
$farRightItem.hide();
} else {
$farLeftItem.hide();
$farRightItem.show();
}
//prepare $itemsToAnimate
for (var i = 0; i < o.animatedItemCount -1; i++) {
this.$itemsToAnimate.push(this.$items.eq(this.adjustIndex(o.centerItemIndex - Math.floor(o.animatedItemCount/2) +i +directionModifier)));
};
}
if (this.currentSubPosition <= o.animationStepCount) {
//animationLoop
for (var i = 0; i < this.$itemsToAnimate.length; i++) {
if (direction == 'left') {
this.$itemsToAnimate[i].css(this.getPositionCss(i, this.currentSubPosition));
} else {
this.$itemsToAnimate[i].css(this.getPositionCss(i, o.animationStepCount-this.currentSubPosition));
}
};
this.currentSubPosition += 1;
this.myTimeout = setTimeout(function(that, direction) {
return function() {
that.animationLoop(direction);
};
}(this, direction), 1000/o.animationStepCount);
} else {
//end animation
this.isCurrentlyBeingAnimated = false;
this.currentSubPosition = 1;
this.$itemsToAnimate = [];
clearTimeout(this.myTimeout);
if (direction == 'left') {
this.distanceToAnimate += 1;
o.centerItemIndex = this.adjustIndex(o.centerItemIndex -1);
} else {
this.distanceToAnimate -= 1;
o.centerItemIndex = this.adjustIndex(o.centerItemIndex +1);
}
this.adjustItemPositions();
//start new animation
if (this.distanceToAnimate < 0) {
this.animationLoop('left');
}
if (this.distanceToAnimate > 0) {
this.animationLoop('right');
}
}
};
$.fn.sashnCarousel = function(option) {
return this.each(function() {
var pluginName = 'svm.sashnCarousel'
, instance = $(this).data(pluginName);
if (!instance) {
if (typeof option != 'object') {
option = {};
}
$(this).data(pluginName, (new SashnCarousel(this, option)))
} else if (typeof instance[option] === 'function') {
instance[option]();
}
})
}
// Constructor nachforschen
// ====================
$.fn.sashnCarousel.Constructor = SashnCarousel;
// NO CONFLICT nachforschen
// ====================
}(window.jQuery));