-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscroll.js
40 lines (34 loc) · 1.07 KB
/
scroll.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
var scrolldelay;
/*
* Constant scrolling
*/
var scroll = function(amt) {
window.scrollBy(0,amt);
}
// Scroll Fusion
var fusedScroll = function() {
var windowHeight = $(window).height();
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + windowHeight;
var yInView = finalY - docViewTop;
var idealBottom = BOTTOM_REGION * windowHeight;
var idealTop = TOP_REGION * windowHeight;
var delta = 0;
if (yInView > idealBottom) {
var slope = getSlope(idealBottom, IDEAL_SCROLL_DELTA, windowHeight, MAX_SCROLL_DELTA)
delta = solveForY(slope, windowHeight, MAX_SCROLL_DELTA, yInView);
} else if (yInView < idealTop) {
var slope = getSlope(0, MIN_SCROLL_DELTA, idealTop, IDEAL_SCROLL_DELTA);
delta = solveForY(slope, idealTop, IDEAL_SCROLL_DELTA, yInView);
} else {
delta = IDEAL_SCROLL_DELTA;
}
scroll(SCROLL_AMT_DEFAULT + delta);
}
var getSlope = function (x1, y1, x2, y2) {
return (y2-y1)/(x2-x1);
}
var solveForY = function (slope, x1, y1, x) {
// point slope form: y-y_1 = m(x-x1)
return slope*(x - x1) + y1;
}