-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsimplescrollup.js
More file actions
103 lines (97 loc) · 3.41 KB
/
simplescrollup.js
File metadata and controls
103 lines (97 loc) · 3.41 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
document.addEventListener("DOMContentLoaded", function(){
// Variables
var buttonUp = document.querySelector('a[href="#up"]');
var easings = {
linear(t) {
return t;
},
easeInQuad(t) {
return t * t;
},
easeOutQuad(t) {
return t * (2 - t);
},
easeInOutQuad(t) {
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
},
easeInCubic(t) {
return t * t * t;
},
easeOutCubic(t) {
return (--t) * t * t + 1;
},
easeInOutCubic(t) {
return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
},
easeInQuart(t) {
return t * t * t * t;
},
easeOutQuart(t) {
return 1 - (--t) * t * t * t;
},
easeInOutQuart(t) {
return t < 0.5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t;
},
easeInQuint(t) {
return t * t * t * t * t;
},
easeOutQuint(t) {
return 1 + (--t) * t * t * t * t;
},
easeInOutQuint(t) {
return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t;
}
};
// Functions
/**
* Animated scroll to up
*/
function scrollUp(duration, easing) {
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;
if ('requestAnimationFrame' in window === false) {
window.scroll(0, 0);
return;
}
// Animation
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 * (0 - start)) + start));
if (window.pageYOffset === 0) {
return;
}
requestAnimationFrame(scroll);
}
// Move
scroll();
}
/**
* Show and hide button
*/
function isVisibled() {
var heightHide = parseInt(buttonUp.getAttribute('height-hide')) || 100;
if (document.body.scrollTop > heightHide || document.documentElement.scrollTop > heightHide) {
buttonUp.classList.remove('simplescrollup__button--hide');
buttonUp.classList.add('simplescrollup__button--show');
} else {
// Hide
buttonUp.classList.remove('simplescrollup__button--show');
buttonUp.classList.add('simplescrollup__button--hide');
}
}
// Events
// Click button
buttonUp.addEventListener('click', function() {
// Get attributes
var duration = parseInt(buttonUp.getAttribute('duration')) || 1000;
var easing = buttonUp.getAttribute('easing') || 'easeInOutQuad';
// Run
scrollUp(duration, easing);
});
// Auto show and hide button
window.addEventListener('scroll', isVisibled);
});