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
|
import Scroll from './scroll.js';
let fold;
let was_below_the_fold = false;
const back_to_top = function(scrolltop) {
let is_below_the_fold = scrolltop > fold;
if (is_below_the_fold !== was_below_the_fold) {
document.getElementById('scroll-to-top').classList.toggle('hide', !is_below_the_fold);
was_below_the_fold = is_below_the_fold;
}
};
const ScrollToTop = {
enable() {
var minScrollHeight = Math.min(
document.body.scrollHeight, document.documentElement.scrollHeight,
document.body.offsetHeight, document.documentElement.offsetHeight,
document.body.clientHeight, document.documentElement.clientHeight
);
fold = minScrollHeight - (minScrollHeight / 5); // top of fifth portion!
Scroll.addHandler('back-to-top', back_to_top);
},
disable() {
Scroll.removeHandler('header');
document.getElementById('scroll-to-top').classList.add('hide');
},
moveBack() {
document.getElementById('scroll-to-top').addEventListener('click', (evt) => {
evt.preventDefault();
this.toTop();
});
document.getElementById('scroll-to-top').addEventListener('keypress', (evt) => {
if (evt.code === 'Space') {
this.toTop();
}
});
},
toTop() {
window.scroll({top: 0, left: 0, behavior: 'smooth'});
}
};
export default ScrollToTop;
|