forked from CodeHunger/Jarallax
/
scroll-converter.js
193 lines (159 loc) · 5.1 KB
/
scroll-converter.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
/*
scrollConverter 1.0
https://github.com/koggdal/scroll-converter
Copyright 2011 Johannes Koggdal (http://koggdal.com/)
Developed for BombayWorks (http://bombayworks.com/)
Released under MIT license
*/
window.scrollConverter = (function (window, document, undefined) {
// Private vars
var docElem = document.documentElement,
active = false,
hasDeactivated = false,
eventsBound = false;
// Private methods
var scrollCallback = function (offset, event, callback) {
// Abort the scrolling if it's inactive
if (!active) {
return;
}
var delta, numPixelsPerStep, change, newOffset,
docOffset, scrollWidth, winWidth, maxOffset;
// Set scrolling parameters
delta = 0;
numPixelsPerStep = 10;
// Find the maximum offset for the scroll
docOffset = (docElem ? docElem.offsetWidth : 0) || 0;
scrollWidth = document.body.scrollWidth || 0;
winWidth = docElem ? docElem.clientWidth : 0;
maxOffset = Math.max(docOffset, scrollWidth) - winWidth;
// "Normalize" the wheel value across browsers
// The delta value after this will not be the same for all browsers.
// Instead, it is normalized in a way to try to give a pretty similar feeling in all browsers.
//
// Firefox and Opera
if (event.detail) {
delta = event.detail * -240;
}
// IE, Safari and Chrome
else if (event.wheelDelta) {
delta = event.wheelDelta * 5;
}
// Get the real offset change from the delta
// A positive change is when the user scrolled the wheel up (in regular scrolling direction)
// A negative change is when the user scrolled the wheel down
change = delta / 120 * numPixelsPerStep;
newOffset = offset.x - change;
// Do the scroll if the new offset is positive
if (newOffset >= 0 && newOffset <= maxOffset) {
offset.x = newOffset;
offset.setByScript = true;
window.scrollTo(offset.x, offset.y);
}
// Keep the offset within the boundaries
else if (offset.x !== 0 && offset.x !== maxOffset) {
offset.x = newOffset > maxOffset ? maxOffset : 0;
offset.setByScript = true;
window.scrollTo(offset.x, offset.y);
}
// Fire the callback
if (typeof callback === "function") {
callback(offset);
}
},
getOffset = function (axis) {
axis = axis.toUpperCase();
var pageOffset = "page" + axis + "Offset",
scrollValue = "scroll" + axis,
scrollDir = "scroll" + (axis === "X" ? "Left" : "Top");
// Get the scroll offset for all browsers
return window[pageOffset] || window[scrollValue] || (function () {
var rootElem = document.documentElement || document.body.parentNode;
return ((typeof rootElem[scrollDir] === "number") ? rootElem : document.body)[scrollDir];
}());
},
bindEvents = function (offset, cb) {
var callback = function (e) {
// Fix event object for IE8 and below
e = e || window.event;
// Trigger the scroll behavior
scrollCallback(offset, e, cb);
// Prevent the normal scroll action to happen
if (e.preventDefault && e.stopPropagation) {
e.preventDefault();
e.stopPropagation();
} else {
return false;
}
},
updateOffsetOnScroll = function () {
// Update the offset variable when the normal scrollbar is used
if (!offset.setByScript) {
offset.x = getOffset("x");
offset.y = getOffset("y");
}
offset.setByScript = false;
};
// Safari, Chrome, Opera, IE9+
if (window.addEventListener) {
// Safari, Chrome, Opera, IE9
if ("onmousewheel" in window) {
window.addEventListener("mousewheel", callback, false);
window.addEventListener("scroll", updateOffsetOnScroll, false);
}
// Firefox
else {
window.addEventListener("DOMMouseScroll", callback, false);
window.addEventListener("scroll", updateOffsetOnScroll, false);
}
}
// IE8 and below
else {
document.attachEvent("onmousewheel", callback);
window.attachEvent("onscroll", updateOffsetOnScroll);
}
},
deactivateScrolling = function (e) {
e.preventDefault();
e.stopPropagation();
return false;
};
// Return a public API
return {
// Activate the scrolling switch
// An optional callback can be passed in, which will fire at every scroll update
activate: function (callback) {
// Set state
active = true;
// Bind events if it hasn't been done before
if (!eventsBound) {
var offset = { x: 0, y: 0 };
bindEvents(offset, callback);
eventsBound = true;
}
// Remove event handlers if it was previously deactivated
if (hasDeactivated) {
if (window.addEventListener) {
window.removeEventListener("scroll", deactivateScrolling, true);
} else {
window.detachEvent("onscroll", deactivateScrolling);
}
hasDeactivated = false;
}
},
deactivate: function () {
active = false;
},
deactivateAllScrolling: function () {
// Set state
active = false;
hasDeactivated = true;
// Bind event handlers to disable the scroll
if (window.addEventListener) {
window.addEventListener("scroll", deactivateScrolling, true);
} else {
window.attachEvent("onscroll", deactivateScrolling);
}
}
};
}(window, document));