-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsmoothScroll-ES5.js
More file actions
72 lines (66 loc) · 2.39 KB
/
smoothScroll-ES5.js
File metadata and controls
72 lines (66 loc) · 2.39 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
"use strict";
window.addEventListener("load",function()
{
function scrollIt(destination, duration, easing)
{
if(destination==null)
{
console.log("scroll destination: "+destination);
return;
}
var easings = {
"linear":function(t) {
return t;
},
"smooth":function(t) {
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
}
};
var start = window.pageYOffset;
var startTime = 'now' in window.performance ? performance.now() : new Date().getTime();
var documentHeight = Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);
var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight;
var destinationOffset = typeof destination === 'number' ? destination : destination.offsetTop;
var destinationOffsetToScroll = Math.round(documentHeight - destinationOffset < windowHeight ? documentHeight - windowHeight : destinationOffset);
if ('requestAnimationFrame' in window === false)
{
window.scroll(0, destinationOffsetToScroll);
return;
}
function scroll()
{
var now = 'now' in window.performance ? performance.now() : new Date().getTime();
var time = Math.min(1, ((now - startTime) / duration));
var timeFunction = easings[easing](time);
window.scroll(0, Math.ceil((timeFunction * (destinationOffsetToScroll - start)) + start));
if (Math.abs(window.pageYOffset- destinationOffsetToScroll)<1) {
return;
}
requestAnimationFrame(scroll);
}
scroll();
}
var all_scroll_on_click_elements=document.getElementsByClassName("scrollOnClick");
if(all_scroll_on_click_elements.length!=0)
{
for(var i=0;i<all_scroll_on_click_elements.length;i++)
all_scroll_on_click_elements[i].addEventListener('click',
function(e)
{
var d=e.currentTarget;
var duration=d.getAttribute("duration");
if(duration=="" || duration==null) duration=500;
duration=parseInt(duration);
var easing=d.getAttribute("easing");
if(easing=="" || easing==null) easing="smooth";
var id=d.getAttribute("scrollTo");
scrollIt(
document.getElementById(id),
duration,
easing
);
});
}else{
console.log("WARNING: No elements with class scrollOnClick found.");
}
});