-
Notifications
You must be signed in to change notification settings - Fork 8
/
pivot.js
354 lines (287 loc) · 12.4 KB
/
pivot.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
;(function (win, doc) {
'use strict';
win.pivot = {
'init': init
};
var _prevent_scroll = false;
function init (config) {
var _touch = (!!('ontouchstart' in win) || !!('onmsgesturechange' in win) || !!(navigator.MaxTouchPoints));
var _targets;
var _target;
var _container;
var i = 0;
var j = 0;
if (typeof config === 'undefined' || typeof config !== 'object') { return; }
if ('touch' in config) { _touch = config.touch; }
if ('selector' in config) { _targets = doc.querySelectorAll(config.selector); }
if (_targets.length > 0) {
i = _targets.length;
for (; i > j; j++) {
_target = _targets[j];
_container = _target.parentNode;
handleHover(_target, _container, config, _touch);
}
}
}
function handleHover (target, container, config, touch) {
var _shadow;
var _shine;
var sensitivity = 0;
var perspectiveProp = getProp(['perspective', 'webkitPerspective', 'mozPerspective']);
var transformStyleProp = getProp(['transformStyle', 'webkitTransformStyle', 'mozTransformStyle']);
var transformProp = getProp(['transform', 'webkitTransform', 'mozTransform']);
var backfaceVisProp = getProp(['backfaceVisibility', 'webkitBackfaceVisibility', 'mozBackfaceVisibility']);
var willChangeProp = getProp(['willChange']);
var boxShadowProp = getProp(['boxShadow', 'webkitBoxShadow', 'mozBoxShadow']);
var userSelectProp = getProp(['userSelect', 'webkitUserSelect', 'mozUserSelect']);
var transitionPropertyProp = getProp(['transitionProperty', 'webkitTransitionProperty', 'mozTransitionProperty']);
var transitionDurationProp = getProp(['transitionDuration', 'webkitTransitionDuration', 'mozTransitionDuration']);
var transitionDelayProp = getProp(['transitionDelay', 'webkitTransitionDelay', 'mozTransitionDelay']);
var transitionTimingProp = getProp(['transitionTimingFunction', 'webkitTransitionTimingFunction', 'mozTransitionTimingFunction']);
if (config.perspective && typeof config.perspective === 'number') {
container.style[perspectiveProp] = config.perspective + 'px';
target.style[perspectiveProp] = config.perspective + 'px';
} else {
container.style[perspectiveProp] = '1000px';
target.style[perspectiveProp] = '1000px';
}
container.style[transformStyleProp] = 'preserve-3d';
target.style[transformStyleProp] = 'preserve-3d';
container.style[userSelectProp] = 'none';
target.style[userSelectProp] = 'none';
target.style[transformProp] = 'rotateY(0deg) rotateX(0deg)';
if (config.sensitivity && typeof config.sensitivity === 'number') {
sensitivity = config.sensitivity;
} else {
sensitivity = 20;
}
if (touch) { target.style[backfaceVisProp] = 'hidden'; }
if (config.position && typeof config.position === 'object') {
target.style.position = config.position.method;
target.style.zIndex = config.position.zindex;
} else {
target.style.position = 'relative';
}
if (config.transition && typeof config.transition === 'object') {
target.style[willChangeProp] = config.transition.prop;
target.style[transitionPropertyProp] = config.transition.prop;
target.style[transitionDurationProp] = getUnit(config.transition.duration);
target.style[transitionTimingProp] = getTFunc(config.transition.timing);
} else {
target.style[willChangeProp] = 'transform';
target.style[transitionPropertyProp] = 'transform';
target.style[transitionDurationProp] = '0.2s';
target.style[transitionTimingProp] = 'cubic-bezier(0.3, 1, 0.2, 1)';
}
if (config.shadow) {
_shadow = doc.createElement('div');
_shadow.className = 'shadow';
_shadow.style.position = 'absolute';
_shadow.style.top = '5%';
_shadow.style.left = '5%';
_shadow.style.bottom = '5%';
_shadow.style.right = '5%';
_shadow.style.zIndex = 1;
_shadow.style[transformProp] = 'translateZ(-2px)';
_shadow.style[boxShadowProp] = '0 8px 30px rgba(14, 21, 47, 0.6)';
if (config.transition && typeof config.transition === 'object') {
_shadow.style[willChangeProp] = 'box-shadow, transform';
_shadow.style[transitionPropertyProp] = 'box-shadow';
_shadow.style[transitionDurationProp] = getUnit(config.transition.duration);
_shadow.style[transitionTimingProp] = getTFunc(config.transition.timing);
} else {
_shadow.style[willChangeProp] = 'box-shadow, transform';
_shadow.style[transitionPropertyProp] = 'box-shadow';
_shadow.style[transitionDurationProp] = '0.2s';
_shadow.style[transitionTimingProp] = 'cubic-bezier(0.3, 1, 0.2, 1)';
}
target.appendChild(_shadow);
}
if (config.shine) {
_shine = doc.createElement('div');
_shine.className = 'shine';
_shine.style.position = 'absolute';
_shine.style.top = 0;
_shine.style.left = 0;
_shine.style.bottom = 0;
_shine.style.right = 0;
_shine.style.zIndex = 9;
_shine.style.opacity = 0;
if (config.transition && typeof config.transition === 'object') {
_shine.style[willChangeProp] = 'opacity, transform';
_shine.style[transitionPropertyProp] = 'opacity';
_shine.style[transitionDurationProp] = getUnit(config.transition.duration);
_shine.style[transitionTimingProp] = getTFunc(config.transition.timing);
} else {
_shine.style[willChangeProp] = 'box-shadow, transform';
_shine.style[transitionPropertyProp] = 'box-shadow';
_shine.style[transitionDurationProp] = '0.2s';
_shine.style[transitionTimingProp] = 'cubic-bezier(0.3, 1, 0.2, 1)';
}
target.appendChild(_shine);
}
if (config.child3D && typeof config.child3D === 'number') {
var p = _target.children.length;
var q = 0;
for (; p > q; q++) {
if (!config.shadow || target.children[q].className !== _shadow.className) {
if (!config.shine || target.children[q].className !== _shine.className) {
target.children[q].style[transformProp] = 'translateZ(' + config.child3D + 'px)';
}
}
}
}
function enter () {
if (config.hoverClass && config.hoverInClass) {
target.className += ' ' + config.hoverClass + ' ' + config.hoverInClass;
setTimeout(function () {
target.className = removeClass(target.className, config.hoverInClass);
}, 1000);
} else if (config.hoverClass) {
target.className += ' ' + config.hoverClass;
} else if (config.hoverInClass) {
target.className += ' ' + config.hoverInClass;
setTimeout(function () {
target.className = removeClass(target.className, config.hoverInClass);
}, 1000);
}
}
function move (e) {
var w = container.offsetWidth;
var h = container.offsetHeight;
var rect = target.getBoundingClientRect();
var ox = touch ? e.touches[0].clientX - rect.left : e.offsetX;
var oy = touch ? e.touches[0].clientY - rect.top : e.offsetY;
var ax = config.invert ? -((w / 2) - ox) / sensitivity : ((w / 2) - ox) / sensitivity;
var ay = config.invert ? ((h / 2) - oy) / sensitivity : -((h / 2) - oy) / sensitivity;
var dy = oy - (h / 2);
var dx = ox - (w / 2);
var theta = Math.atan2(dy, dx);
var ang = (theta * (180 / Math.PI)) - 90;
var angle = ang < 0 ? ang + 360 : ang;
if (config.scale) {
target.style[transformProp] = 'rotateY(' + ax + 'deg) rotateX(' + ay + 'deg) scale3d(1.05, 1.05, 1.05)';
} else {
target.style[transformProp] = 'rotateY(' + ax + 'deg) rotateX(' + ay + 'deg)';
}
if (config.shadow) {
_shadow.style[boxShadowProp] = '0 24px 48px rgba(14, 21, 47, 0.4), 0 12px 24px rgba(14, 21, 47, 0.4)';
}
if (config.shine) {
_shine.style.opacity = 1;
_shine.style.backgroundImage = 'linear-gradient(' + angle + 'deg, rgba(230, 230, 230, ' + oy / h * 0.5 +') 0%, transparent 80%)';
}
}
function leave () {
if (config.shadow) {
_shadow.style[boxShadowProp] = '0 8px 30px rgba(14, 21, 47, 0.6)';
}
if (!config.persist) {
target.style[transformProp] = 'rotateX(0deg) rotateY(0deg)';
if (config.shine) {
_shine.style.opacity = 0;
}
}
if (config.hoverClass && config.hoverOutClass) {
target.className += ' ' + config.hoverOutClass;
target.className = removeClass(target.className, config.hoverClass);
setTimeout(function () {
target.className = removeClass(target.className, config.hoverOutClass);
}, 1000);
} else if (config.hoverClass) {
target.className = removeClass(target.className, config.hoverClass);
} else if (config.hoverOutClass) {
target.className += ' ' + config.hoverOutClass;
setTimeout(function () {
target.className = removeClass(target.className, config.hoverOutClass);
}, 1000);
}
}
if (touch) {
container.addEventListener('touchstart', function () {
if (!_prevent_scroll) { _prevent_scroll = true; }
return enter();
});
container.addEventListener('touchmove', function (e) {
if (!!_prevent_scroll) { e.preventDefault(); }
return move(e);
});
container.addEventListener('touchend', function () {
if (!!_prevent_scroll) { win.preventScroll = false; }
return leave();
});
} else {
container.addEventListener('mouseenter', function () {
return enter();
});
container.addEventListener('mousemove', function (e) {
return move(e);
});
container.addEventListener('mouseleave', function () {
return leave();
});
}
}
function getProp (props) {
var i = props.length;
var j = 0;
for ( ; i > j; j++) {
if (typeof doc.body.style[props[j]] !== 'undefined') {
return props[j];
}
}
return null;
}
function getUnit (t) {
if (typeof t !== 'number') {
console.warn('Please provide a numeric value');
return '0.2s';
} else if (t > 1 && t <= 50) {
return '0.' + t + 's';
} else if (t > 50) {
return t + 'ms';
} else {
return t + 's';
}
}
function getTFunc (tf) {
var tfl = tf.length;
if (tf.constructor !== Array) {
console.warn('Bad input: expected array');
return 'none';
} else if (tfl === 4) {
if (typeof tf[0] === 'number' && typeof tf[1] === 'number' && typeof tf[2] === 'number' && typeof tf[3] === 'number') {
return 'cubic-bezier(' + tf[0] + ', ' + tf[1] + ', ' + tf[2] + ', ' + tf[3] + ')';
} else {
console.warn('Bad input: expected numbers');
return 'none';
}
} else {
console.warn('Bad input: expected four values');
return 'none';
}
}
function removeClass (cssClasses, cssClass) {
var rxp = new RegExp(cssClass + '\\s*', 'gi');
return cssClasses.replace(rxp, '').replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
}
function rebounce (func) {
var scheduled, context, args, i, j;
return function () {
context = this;
args = [];
i = arguments.length;
j = 0;
for (; j < i; ++j) {
args[j] = arguments[j];
}
if (!!scheduled) {
win.cancelAnimationFrame(scheduled);
}
scheduled = win.requestAnimationFrame(function () {
func.apply(context, args);
scheduled = null;
});
}
}
})(window, document);