This repository was archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmooth.js
More file actions
152 lines (123 loc) · 3.75 KB
/
Copy pathsmooth.js
File metadata and controls
152 lines (123 loc) · 3.75 KB
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
/*
* smooth is a small javascript-file that enables a smooth scrolling on the most actual webbrowsers.
*
* Copyright © 2014 Martin Hodler <martin.hodler@gmail.com>
*
* GNU GPL v3
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
var SMOOTH = function () {
var scrollPos = 0;
var scrollEnd = 0;
var isAnimating = false;
var endTime = 0;
var isIE = false;
var isChrome = false;
function init(e) {
isIE = window.navigator.userAgent.match(/.NET/i) ? true : false;
isChrome = window.navigator.userAgent.match(/Chrome/i) ? true : false;
if (isIE)
setTimeout(function() { calculate(false); }, 100);
else
calculate(false);
}
function resize(e) {
calculate(false);
}
function calculate(posOnly) {
posOnly = (posOnly !== undefined && posOnly === true) ? true : false;
if (isChrome) {
scrollPos = document.body.scrollTop;
if (!posOnly)
scrollEnd = document.body.scrollTop;
} else {
scrollPos = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
if (!posOnly) {
scrollEnd = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
}
}
}
function scroll(e) {
if (isIE) {
if (document.compatMode == "CSS1Compat")
calculate(true);
}
var delta = e.wheelDelta;
if (typeof delta === "undefined")
delta = e.detail * -40;
delta = delta * -1;
scrollEnd += delta;
if (scrollEnd < 0)
scrollEnd = 0;
if (scrollEnd > (document.clientHeight - window.innerHeight) + 3)
scrollEnd = (document.clientHeight - window.innerHeight) + 3;
endTime = new Date().getTime() + 1000;
if (!isAnimating)
scrollStep();
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
function scrollStep() {
isAnimating = true;
time = new Date().getTime();
var delta = endTime - time;
scrollPos += (scrollEnd - scrollPos) / 5000 * delta;
if (scrollPos < 5)
scrollPos = Math.floor(scrollPos);
else
scrollPos = Math.round(scrollPos);
if (scrollPos > (document.body.clientHeight - window.innerHeight) - 5)
scrollPos = Math.floor(scrollPos);
else
scrollPos = Math.round(scrollPos);
var top = 0;
if (isChrome) {
document.body.scrollTop = scrollPos;
top = document.body.scrollTop;
} else {
document.documentElement.scrollTop = scrollPos;
top = document.documentElement.scrollTop;
}
if (delta > 0 && top == scrollPos) {
setTimeout(scrollStep, 32);
} else {
scrollPos = top;
scrollEnd = top;
isAnimating = false;
}
}
var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel";
if (document.addEventListener) {
document.addEventListener(mousewheelevt, function(e){ scroll(e); }, false);
addEventListener("load", function(e){ init(e); }, false);
} else if (document.attachEvent) {
document.attachEvent("on"+mousewheelevt, function(e){ scroll(e); } );
window.attachEvent("onload", function(e){ init(e); } );
}
function getScrollPosition() {
return scrollPos;
}
function getScrollEnd() {
return scrollEnd;
}
return {
scrollPosition : getScrollPosition,
scrollEnd : getScrollEnd
};
}();