forked from vanderlee/coverflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.coverflow.js
371 lines (316 loc) · 11.1 KB
/
jquery.coverflow.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*jslint devel: true, bitwise: true, regexp: true, browser: true, confusion: true, unparam: true, eqeq: true, white: true, nomen: true, plusplus: true, maxerr: 50, indent: 4 */
/*globals jQuery */
/*!
* Coverflow
*
* Copyright (c) 2013-2015 Martijn W. van der Lee
* Licensed under the MIT.
*/
/* Lightweight and flexible coverflow effect using CSS3 transforms.
* For modern browsers with some amount of graceful degradation.
* Optional support for jQuery.interpolate() plugin.
* Optional support for .reflect() plugins.
*
* Requires jQuery 1.7+ and jQueryUI 1.9+.
* Recommended jQuery 1.8+ and jQueryUI 1.9+.
*/
;(function($, undefined) {
"use strict";
var sign = function(number) {
return number < 0 ? -1 : 1;
},
scl = function(number, fromMin, fromMax, toMin, toMax) {
return ((number - fromMin) * (toMax - toMin) / (fromMax - fromMin)) + toMin;
},
wheelEvents = ('onwheel' in document) ? 'wheel' : 'mousewheel', // FF
getWheel = function(event) {
if ('deltaY' in event.originalEvent) {
return 0 - event.originalEvent.deltaY;
} else if ('wheelDelta' in event.originalEvent) {
return event.originalEvent.wheelDelta; // IE
}
};
$.widget("vanderlee.coverflow", {
options: {
animateComplete: undefined,
animateStep: undefined,
density: 1,
duration: 'normal',
easing: undefined,
enableKeyboard: 'both', // true, false, 'both', 'focus', 'hover', 'none'
enableClick: true,
enableWheel: true,
index: 0,
innerAngle: -75,
innerCss: undefined,
innerOffset: 100 / 3,
innerScale: 0.75,
outerAngle: -30,
outerCss: undefined,
outerScale: 0.25,
selectedCss: undefined,
visible: 'density', // 'density', 'all', NNN (exact)
width: undefined,
change: undefined, // Whenever index is changed
confirm: undefined, // Whenever clicking on the current item
select: undefined // Whenever index is set (also on init)
},
_window_handler_resize: null,
_window_handler_keydown: null,
_create: function() {
var that = this,
covers = that._getCovers(),
images = covers.filter('img').add('img', covers).filter(function() {
return !(this.complete || this.height > 0);
}),
maxHeight = covers.height(),
height;
// Internal event prefix
that.widgetEventPrefix = 'vanderlee-coverflow';
that.hovering = false;
that.pagesize = 1;
that.currentIndex = that.options.index;
// Fix height
that.element.height(maxHeight);
images.load(function() {
height = that._getCovers().height();
if (height > maxHeight) {
maxHeight = height;
that.element.height(maxHeight);
}
});
// Hide all covers and set position to absolute
covers.hide();
// Add tabindex and autofocus if needed.
if (this.element.not(':tabbable')) {
this.element.attr('tabIndex', -1);
if (this.element.attr('autofocus')) {
this.element.focus();
}
}
// Enable click-jump
that.element.on('mousedown tap', '> *', function(event) {
if (that.options.enableClick) {
var index = that._getCovers().index(this);
if (index === that.currentIndex) {
that._callback('confirm', event);
} else {
that._setIndex(index, true);
}
}
});
// Mousewheel
that.element.on(wheelEvents, function(event) {
if (that.options.enableWheel) {
var delta = getWheel(event) > 0 ? 1 : -1;
event.preventDefault();
that._setIndex(that.options.index - delta, true);
}
});
// Swipe
if ($.isFunction(that.element.swipe)) {
that.element.swipe({
allowPageScroll: "vertical",
swipe: function(event, direction, distance, duration, fingerCount) {
var count = Math.round((direction==="left"? 1 : (direction==="right"? -1 : 0 )) * 1.25 * that.pagesize * distance / that.element.width());
that._setIndex(that.options.index + count, true);
}
});
}
// Keyboard
// that.element.hover(
// function() { that.hovering = true; }
// , function() { that.hovering = false; }
// );
// Refresh on resize
that._window_handler_resize = function() {
that.refresh();
};
$(window).on('resize', that._window_handler_resize);
that._window_handler_keydown = function(event) {
if (($.inArray(that.options.enableKeyboard, [true, 'both', 'focus']) >= 0 && that.element.is(':focus'))
|| ($.inArray(that.options.enableKeyboard, [true, 'both', 'hover']) >= 0 >= 0 && that.element.is(':hover'))) {
switch (event.which) {
case 36: // home
event.preventDefault();
that._setIndex(0, true);
break;
case 35: // end
event.preventDefault();
that._setIndex(that._getCovers().length - 1, true);
break;
case 38: // up
case 37: // left
event.preventDefault();
that._setIndex(that.options.index - 1, true);
break;
case 40: // down
case 39: // right
event.preventDefault();
that._setIndex(that.options.index + 1, true);
break;
case 33: // page up (towards home)
event.preventDefault();
that._setIndex(that.options.index - that.pagesize, true);
break;
case 34: // page down (towards end)
event.preventDefault();
that._setIndex(that.options.index + that.pagesize, true);
break;
}
}
};
$(window).on('keydown', that._window_handler_keydown);
// Initialize
that._setIndex(that.options.index, false, true);
return that;
},
/**
* Destroy this object
* @returns {undefined}
*/
_destroy: function() {
$(window).off('resize', this._window_handler_resize);
$(window).off('keydown', this._window_handler_keydown);
this.element.height('');
},
/**
* Returns the currently selected cover
* @returns {jQuery} jQuery object
*/
cover: function() {
return $(this._getCovers()[this.options.index]);
},
/**
*
* @returns {unresolved}
*/
_getCovers: function() {
return $('> *', this.element);
},
_setIndex: function(index, animate, initial) {
var that = this,
covers = that._getCovers();
index = Math.max(0, Math.min(index, covers.length - 1));
if (index !== that.options.index) {
// Fix reflections
covers.css('position', 'absolute');
this._frame(that.options.index);
if (animate === true || that.options.duration === 0) {
that.options.index = Math.round(index);
var duration = typeof that.options.duration === "number"
? that.options.duration
: jQuery.fx.speeds[that.options.duration] || jQuery.fx.speeds._default;
this.refresh(duration, that.options.index);
} else {
that.options.index = Math.round(index);
that.refresh(0);
}
} else if (initial === true) {
that.refresh();
that._callback('select');
}
},
_callback: function(callback, event) {
this._trigger(callback, event, [this._getCovers().get(this.currentIndex), this.currentIndex]);
},
index: function(index) {
if (index === undefined) {
return this.options.index;
}
while (index < 0) {
index += this._getCovers().length;
}
this._setIndex(index, true);
},
_frame: function(frame) {
frame = frame.toFixed(6);
var that = this,
covers = that._getCovers(),
count = covers.length,
parentWidth = that.element.innerWidth(),
coverWidth = that.options.width || covers.first().outerWidth(),
visible = that.options.visible === 'density' ? Math.round(parentWidth * that.options.density / coverWidth)
: $.isNumeric(that.options.visible) ? that.options.visible
: count,
parentLeft = that.element.position().left - ((1 - that.options.outerScale) * coverWidth * 0.5),
space = (parentWidth - (that.options.outerScale * coverWidth)) * 0.5;
that.pagesize = visible;
covers.removeClass('current').each(function(index, cover) {
var $cover = $(cover),
position = index - frame,
offset = Math.min(Math.max(-1., position / visible), 1),
isMiddle = position == 0,
zIndex = count - Math.abs(Math.round(position)),
isVisible = Math.abs(position) <= visible,
sin = Math.sin(offset * Math.PI * 0.5),
cos = Math.cos(offset * Math.PI * 0.5),
left = sign(sin) * scl(Math.abs(sin), 0, 1, that.options.innerOffset * that.options.density, space),
scale = isVisible ? scl(Math.abs(cos), 1, 0, that.options.innerScale, that.options.outerScale) : 0,
angle = sign(sin) * scl(Math.abs(sin), 0, 1, that.options.innerAngle, that.options.outerAngle),
css = isMiddle ? that.options.selectedCss || {}
: ( $.interpolate && that.options.outerCss && !$.isEmptyObject(that.options.outerCss) ? (
isVisible ? $.interpolate(that.options.innerCss || {}, that.options.outerCss, Math.abs(sin))
: that.options.outerCss
) : {}
),
transform;
// bad behaviour for being in the middle
if (Math.abs(position) < 1) {
angle = 0 - (0 - angle) * Math.abs(position);
scale = 1 - (1 - scale) * Math.abs(position);
left = 0 - (0 - left) * Math.abs(position);
}
//@todo Test CSS for middle behaviour (or does $.interpolate handle it?)
transform = 'scale(' + scale + ',' + scale + ') perspective(' + (parentWidth * 0.5) + 'px) rotateY(' + angle + 'deg)';
$cover[isMiddle ? 'addClass' : 'removeClass']('current');
$cover[isVisible ? 'show' : 'hide']();
$cover.css($.extend(css, {
'left': parentLeft + space + left,
'z-index': zIndex,
'-webkit-transform': transform,
'-ms-transform': transform,
'transform': transform
}));
// Optional callback
that._trigger('animateStep', null, [cover, offset, isVisible, isMiddle, sin, cos]);
if (frame == that.options.index) {
// Optional callback
that._trigger('animateComplete', null, [cover, offset, isVisible, isMiddle, sin, cos]);
}
});
},
refresh: function(duration, index) {
var that = this,
previous = that.currentIndex,
covers = that._getCovers(),
covercount = covers.length,
triggered = false;
covers.css('position', 'absolute');
that.element.stop().animate({
'__coverflow_frame': index || that.options.index
}, {
'easing': that.options.easing,
'duration': duration || 0,
'step': function(now, fx) {
that._frame(now);
that.currentIndex = Math.max(0, Math.min(Math.round(now), covercount - 1));
if (previous !== that.currentIndex) {
previous = that.currentIndex;
that._callback('change');
if (that.currentIndex === that.options.index) {
triggered = true;
}
}
},
'complete': function() {
that.currentIndex = that.options.index;
if (!triggered) {
that._callback('change');
}
that._callback('select');
}
});
}
});
}(jQuery));