/
jquery.scrollorama.js
519 lines (467 loc) · 16.9 KB
/
jquery.scrollorama.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
scrollorama - The jQuery plugin for doing cool scrolly stuff
by John Polacek (@johnpolacek)
Dual licensed under MIT and GPL.
*/
(function($) {
$.scrollorama = function(options) {
var scrollorama = this,
blocks = [],
browserPrefix = '',
onBlockChange = function() {},
latestKnownScrollY = 0,
ticking = false,
requestAnimFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
},
defaults = {offset:0, enablePin: true};
scrollorama.settings = $.extend({}, defaults, options);
scrollorama.blockIndex = 0;
if (options.blocks === undefined) { alert('ERROR: Must assign blocks class selector to scrollorama plugin'); }
// PRIVATE FUNCTIONS
function init() {
var i, block, didScroll, marginTop = false;
if (typeof scrollorama.settings.blocks === 'string') { scrollorama.settings.blocks = $(scrollorama.settings.blocks); }
// set browser prefix
if ($.browser.mozilla) { browserPrefix = '-moz-'; }
if ($.browser.webkit) { browserPrefix = '-webkit-'; }
if ($.browser.opera) { browserPrefix = '-o-'; }
if ($.browser.msie) { browserPrefix = '-ms-'; }
// create blocks array to contain animation props
$('body').css('position','relative');
for (i=0; i<scrollorama.settings.blocks.length; i++) {
block = scrollorama.settings.blocks.eq(i);
marginTop = block.css('margin-top');
blocks.push({
block: block,
top: block.offset().top - (!Boolean(marginTop) ? parseInt(marginTop, 10) : 0),
pin: 0,
animations:[]
});
}
// convert block elements to absolute position
if (scrollorama.settings.enablePin.toString() === 'true') {
for (i=0; i<blocks.length; i++) {
blocks[i].block
.css('position', 'absolute')
.css('top', blocks[i].top);
}
}
$('body').prepend('<div id="scroll-wrap"></div>');
latestKnownScrollY = 0;
ticking = false;
$(window).on( 'scroll.scrollorama', onScroll );
}
function onScroll() {
latestKnownScrollY = window.scrollY;
requestTick();
}
function requestTick() {
if(!ticking) {
requestAnimFrame(function(){
onScrollorama();
update();
});
}
ticking = true;
}
function update() {
// reset the tick so we can
// capture the next onScroll
ticking = false;
}
function onScrollorama() {
var scrollTop = $(window).scrollTop(),
currBlockIndex = getCurrBlockIndex(scrollTop),
i, j, anim, startAnimPos, endAnimPos, animPercent, animVal;
// update all animations
for (i=0; i<blocks.length; i++) {
// go through the animations for each block
if (blocks[i].animations.length) {
for (j=0; j<blocks[i].animations.length; j++) {
anim = blocks[i].animations[j];
// if above current block, settings should be at start value
if (i > currBlockIndex) {
if (currBlockIndex !== i-1 && anim.baseline !== 'bottom') {
setProperty(anim.element, anim.property, anim.startVal);
}
if (blocks[i].pin) {
blocks[i].block
.css('position', 'absolute')
.css('top', blocks[i].top);
}
}
// if below current block, settings should be at end value
// unless on an element that gets animated when it hits the bottom of the viewport
else if (i < currBlockIndex) {
setProperty(anim.element, anim.property, anim.endVal);
if (blocks[i].pin) {
blocks[i].block
.css('position', 'absolute')
.css('top', (blocks[i].top + blocks[i].pin));
}
}
// otherwise, set values per scroll position
if (i === currBlockIndex || (currBlockIndex === i-1 && anim.baseline === 'bottom')) {
// if block gets pinned, set position fixed
if (blocks[i].pin && currBlockIndex === i) {
blocks[i].block
.css('position', 'fixed')
.css('top', 0);
}
// set start and end animation positions
startAnimPos = blocks[i].top + anim.delay;
if (anim.baseline === 'bottom') { startAnimPos -= $(window).height(); }
endAnimPos = startAnimPos + anim.duration;
// if scroll is before start of animation, set to start value
if (scrollTop < startAnimPos) {
setProperty(anim.element, anim.property, anim.startVal);
}
// if scroll is after end of animation, set to end value
else if (scrollTop > endAnimPos) {
setProperty(anim.element, anim.property, anim.endVal);
if (blocks[i].pin) {
blocks[i].block
.css('position', 'absolute')
.css('top', (blocks[i].top + blocks[i].pin));
}
}
// otherwise, set value based on scroll
else {
// calculate percent to animate
animPercent = (scrollTop - startAnimPos) / anim.duration;
// account for easing if there is any
if ( anim.easing && $.isFunction( $.easing[anim.easing] ) ) {
animPercent = $.easing[anim.easing]( animPercent, animPercent*1000, 0, 1, 1000 );
}
// then multiply the percent by the value range and calculate the new value
animVal = anim.startVal + (animPercent * (anim.endVal - anim.startVal));
setProperty(anim.element, anim.property, animVal);
}
}
}
}
}
// update blockIndex and trigger event if changed
if (scrollorama.blockIndex !== currBlockIndex) {
scrollorama.blockIndex = currBlockIndex;
onBlockChange();
}
}
function getCurrBlockIndex(scrollTop) {
var currBlockIndex = 0, i;
for (i=0; i<blocks.length; i++) {
// check if block is in view
if (blocks[i].top <= scrollTop - scrollorama.settings.offset) { currBlockIndex = i; }
}
return currBlockIndex;
}
function setProperty(target, prop, val) {
var scaleCSS, currentPosition;
if (prop === 'rotate' || prop === 'zoom' || prop === 'scale') {
if (prop === 'rotate') {
target.css(browserPrefix+'transform', 'rotate('+val+'deg)');
} else if (prop === 'zoom' || prop === 'scale') {
scaleCSS = 'scale('+val+')';
if (browserPrefix !== '-ms-') {
target.css(browserPrefix+'transform', scaleCSS);
} else {
target.css('zoom', scaleCSS);
}
}
}
else if(prop === 'background-position-x' || prop === 'background-position-y' ) {
currentPosition = target.css('background-position').split(' ');
if(prop === 'background-position-x') {
target.css('background-position',val+'px '+currentPosition[1]);
}
if(prop === 'background-position-y') {
target.css('background-position', currentPosition[0]+' '+val+'px');
}
}
else if(prop === 'text-shadow' ) {
target.css(prop,'0px 0px '+val+'px #ffffff');
} else {
target.css(prop, val);
}
}
// PUBLIC FUNCTIONS
scrollorama.animate = function(target) {
var targetIndex,
targetBlock,
anim,
offset,
i, j;
/*
target = animation target
arguments = array of animation parameters
anim = object that contains all animation params (created from arguments)
offset = positioning helper for pinning
animation parameters:
delay = amount of scrolling (in pixels) before animation starts
duration = amount of scrolling (in pixels) over which the animation occurs
property = css property being animated
start = start value of the property
end = end value of the property
pin = pin block during animation duration (applies to all animations within block)
baseline = top (default, when block reaches top of viewport) or bottom (when block first comies into view)
easing = just like jquery's easing functions
*/
// if string, convert to DOM object
if (typeof target === 'string') { target = $(target); }
// find block of target
for (i=0; i<blocks.length; i++) {
if (blocks[i].block.has(target).length) {
targetBlock = blocks[i];
targetIndex = i;
}
}
// add each animation to the blocks animations array from function arguments
for (i=1; i<arguments.length; i++) {
anim = arguments[i];
// for top/left/right/bottom, set relative positioning if static
if (anim.property === 'top' || anim.property === 'left' || anim.property === 'bottom' || anim.property === 'right' ) {
if (target.css('position') === 'static') { target.css('position','relative'); }
// set anim.start, anim.end defaults
cssValue = parseInt(target.css(anim.property),10);
if (anim.start === undefined) {
anim.start = isNaN(cssValue) ? 0 : cssValue;
} else if (anim.end === undefined) {
anim.end = isNaN(cssValue) ? 0 : cssValue;
}
}
// set anim.start/anim.end defaults for rotate, zoom/scale, letter-spacing
if (anim.property === 'rotate') {
if (anim.start === undefined) { anim.start = 0; }
if (anim.end === undefined) { anim.end = 0; }
} else if (anim.property === 'zoom' || anim.property === 'scale' ) {
if (anim.start === undefined) { anim.start = 1; }
if (anim.end === undefined) { anim.end = 1; }
} else if (anim.property === 'letter-spacing' && target.css(anim.property)) {
if (anim.start === undefined) { anim.start = 1; }
if (anim.end === undefined) { anim.end = 1; }
}
if (anim.baseline === undefined) {
if (anim.pin || targetBlock.pin || targetIndex === 0) {
anim.baseline = 'top';
} else {
anim.baseline = 'bottom';
}
}
if (anim.delay === undefined) { anim.delay = 0; }
targetBlock.animations.push({
element: target,
delay: anim.delay,
duration: anim.duration,
property: anim.property,
startVal: anim.start !== undefined ? anim.start : parseInt(target.css(anim.property),10), // if undefined, use current css value
endVal: anim.end !== undefined ? anim.end : parseInt(target.css(anim.property),10), // if undefined, use current css value
baseline: anim.baseline !== undefined ? anim.baseline : 'bottom',
easing: anim.easing
});
if (anim.pin) {
if (targetBlock.pin < anim.duration + anim.delay) {
offset = anim.duration + anim.delay - targetBlock.pin;
targetBlock.pin += offset;
// adjust positions of blocks below target block
for (j=targetIndex+1; j<blocks.length; j++) {
blocks[j].top += offset;
blocks[j].block.css('top', blocks[j].top);
}
}
}
}
onScrollorama();
};
// function for passing blockChange event callback
scrollorama.onBlockChange = function(f) {
onBlockChange = f;
};
// function for getting an array of scrollpoints
// (top of each animation block and animation element scroll start point)
scrollorama.getScrollpoints = function() {
var scrollpoints = [],i,j,anim;
for (i=0; i<blocks.length; i++) {
scrollpoints.push(blocks[i].top);
// go through the animations for each block
if (blocks[i].animations.length && blocks[i].pin > 0) {
for (j=0; j<blocks[i].animations.length; j++) {
anim = blocks[i].animations[j];
scrollpoints.push(blocks[i].top + anim.delay + anim.duration);
}
}
}
// make sure scrollpoints are in numeric order
scrollpoints.sort(function(a,b) {return a - b;});
return scrollpoints;
};
// Remove scrollorama
scrollorama.destroy = function () {
// Remove animations
for (i=0; i<blocks.length; i++) {
// Remove CSS rules
blocks[i].block.css({
top: '',
position: ''
});
// Remove scrolloroma-specific attributes
delete blocks[i].animations;
delete blocks[i].top;
delete blocks[i].pin;
}
// Unbind the window scroll event
$(window).off('scroll.scrollorama');
$('#scroll-wrap').remove();
// Remove the scrolloroma object
delete scrollorama;
};
// INIT
init();
return scrollorama;
};
//
// Easing functions from jQuery UI
//
$.extend($.easing, {
def: 'easeOutQuad',
swing: function (x, t, b, c, d) {
//alert($.easing.default);
return $.easing[$.easing.def](x, t, b, c, d);
},
easeInQuad: function (x, t, b, c, d) {
return c*(t/=d)*t + b;
},
easeOutQuad: function (x, t, b, c, d) {
return -c *(t/=d)*(t-2) + b;
},
easeInOutQuad: function (x, t, b, c, d) {
if ((t/=d/2) < 1) { return c/2*t*t + b; }
return -c/2 * ((--t)*(t-2) - 1) + b;
},
easeInCubic: function (x, t, b, c, d) {
return c*(t/=d)*t*t + b;
},
easeOutCubic: function (x, t, b, c, d) {
return c*((t=t/d-1)*t*t + 1) + b;
},
easeInOutCubic: function (x, t, b, c, d) {
if ((t/=d/2) < 1) { return c/2*t*t*t + b; }
return c/2*((t-=2)*t*t + 2) + b;
},
easeInQuart: function (x, t, b, c, d) {
return c*(t/=d)*t*t*t + b;
},
easeOutQuart: function (x, t, b, c, d) {
return -c * ((t=t/d-1)*t*t*t - 1) + b;
},
easeInOutQuart: function (x, t, b, c, d) {
if ((t/=d/2) < 1) { return c/2*t*t*t*t + b; }
return -c/2 * ((t-=2)*t*t*t - 2) + b;
},
easeInQuint: function (x, t, b, c, d) {
return c*(t/=d)*t*t*t*t + b;
},
easeOutQuint: function (x, t, b, c, d) {
return c*((t=t/d-1)*t*t*t*t + 1) + b;
},
easeInOutQuint: function (x, t, b, c, d) {
if ((t/=d/2) < 1) { return c/2*t*t*t*t*t + b; }
return c/2*((t-=2)*t*t*t*t + 2) + b;
},
easeInSine: function (x, t, b, c, d) {
return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
},
easeOutSine: function (x, t, b, c, d) {
return c * Math.sin(t/d * (Math.PI/2)) + b;
},
easeInOutSine: function (x, t, b, c, d) {
return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
},
easeInExpo: function (x, t, b, c, d) {
return (t===0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
},
easeOutExpo: function (x, t, b, c, d) {
return (t===d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
},
easeInOutExpo: function (x, t, b, c, d) {
if (t===0) { return b; }
if (t===d) { return b+c; }
if ((t/=d/2) < 1) { return c/2 * Math.pow(2, 10 * (t - 1)) + b; }
return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
},
easeInCirc: function (x, t, b, c, d) {
return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
},
easeOutCirc: function (x, t, b, c, d) {
return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
},
easeInOutCirc: function (x, t, b, c, d) {
if ((t/=d/2) < 1) { return -c/2 * (Math.sqrt(1 - t*t) - 1) + b; }
return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
},
easeInElastic: function (x, t, b, c, d) {
var s=1.70158,p=0,a=c;
if (t===0) { return b; }
if ((t/=d)===1) { return b+c; }
if (!p) { p=d*0.3; }
if (a < Math.abs(c)) { a=c; s=p/4; }
else{ s = p/(2*Math.PI) * Math.asin (c/a); }
return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
},
easeOutElastic: function (x, t, b, c, d) {
var s=1.70158,p=0,a=c;
if (t===0) { return b; }
if ((t/=d)===1) { return b+c; }
if (!p) { p=d*0.3; }
if (a < Math.abs(c)) { a=c; s=p/4; }
else { s = p/(2*Math.PI) * Math.asin (c/a); }
return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
},
easeInOutElastic: function (x, t, b, c, d) {
var s=1.70158,p=0,a=c;
if (t===0) { return b; }
if ((t/=d/2)===2) { return b+c; }
if (!p) { p=d*(0.3*1.5); }
if (a < Math.abs(c)) { a=c; s=p/4; }
else { s = p/(2*Math.PI) * Math.asin (c/a); }
if (t < 1) { return -0.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b; }
return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*0.5 + c + b;
},
easeInBack: function (x, t, b, c, d, s) {
if (s === undefined) { s = 1.70158; }
return c*(t/=d)*t*((s+1)*t - s) + b;
},
easeOutBack: function (x, t, b, c, d, s) {
if (s === undefined) { s = 1.70158; }
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
},
easeInOutBack: function (x, t, b, c, d, s) {
if (s === undefined) { s = 1.70158; }
if ((t/=d/2) < 1) { return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; }
return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
},
easeInBounce: function (x, t, b, c, d) {
return c - $.easing.easeOutBounce (x, d-t, 0, c, d) + b;
},
easeOutBounce: function (x, t, b, c, d) {
if ((t/=d) < (1/2.75)) {
return c*(7.5625*t*t) + b;
} else if (t < (2/2.75)) {
return c*(7.5625*(t-=(1.5/2.75))*t + 0.75) + b;
} else if (t < (2.5/2.75)) {
return c*(7.5625*(t-=(2.25/2.75))*t + 0.9375) + b;
} else {
return c*(7.5625*(t-=(2.625/2.75))*t + 0.984375) + b;
}
},
easeInOutBounce: function (x, t, b, c, d) {
if (t < d/2) { return $.easing.easeInBounce (x, t*2, 0, c, d) * 0.5 + b; }
return $.easing.easeOutBounce (x, t*2-d, 0, c, d) * 0.5 + c*0.5 + b;
}
});
})(jQuery);