-
Notifications
You must be signed in to change notification settings - Fork 25
/
overscroll.js
105 lines (82 loc) · 2.92 KB
/
overscroll.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
/*!
* Overscroll.
*
* MIT licensed
* Copyright (C) 2015 Tim Holman, http://tholman.com
*/
/*********************************************
* Utils
*********************************************/
function getSupportedTransform() {
var prefixes = 'transform WebkitTransform MozTransform OTransform msTransform'.split(' ');
for(var i = 0; i < prefixes.length; i++) {
if(document.createElement('div').style[prefixes[i]] !== undefined) {
return prefixes[i];
}
}
return false;
}
/*********************************************
* Overscroll
*********************************************/
function Overscroll() {
var scrollPosition = 0;
var windowHeight = 0;
var shakeYourBody = document.body;
var windows95 = window;
var transform = getSupportedTransform();
var elements = {top: [], bottom: []};
this.init = function() {
this.window = window;
this.onScroll();
this.onResize();
this.scrollEvent = this.window.addEventListener("scroll", this.onScroll);
this.resizeEvent = this.window.addEventListener("resize", this.onResize);
}
this.bindElement = function(element, position, delta) {
// Default data!
delta = delta || 1;
position = position || 'top';
// Only bind events once there is something to overscroll.
if( elements.top.length === 0 && elements.bottom.length === 0 ) {
this.init();
}
var elementObject = {domElement: element, height: element.clientHeight, delta: delta};
elements[position].push(elementObject);
}
this.onScroll = function() {
scrollPosition = windows95.pageYOffset || shakeYourBody.scrollTop;
doTheThing();
}
// @TODO, Should I check elements heights here, incase they change?
this.onResize = function() {
windowHeight = windows95.innerHeight;
}
// Zhu Li!
var doTheThing = function() {
var i;
// @TODO: Could this be cleaner, somehow?
// @TODO: Should this use RAF rather than not... its not very intensive
if( scrollPosition <= 0 ) {
for( i = 0; i < elements.top.length; i++ ) {
var thisElement = elements.top[i];
var movement = ( -scrollPosition * thisElement.delta );
// Don't allow element to overreach its height.
if( movement > thisElement.height ) {
movement = thisElement.height;
}
thisElement.domElement.style[transform] = 'translateY(' + ( movement ) + 'px )';
}
} else if ( scrollPosition >= windowHeight ) {
for( i = 0; i < elements.bottom.length; i++ ) {
var thisElement = elements.bottom[i];
var movement = ( ( scrollPosition - windowHeight ) * thisElement.delta );
// Don't allow element to overreach its height.
if( movement > thisElement.height ) {
movement = thisElement.height;
}
thisElement.domElement.style[transform] = 'translateY(' + (-movement ) + 'px )';
}
}
}
}