hexo-theme-keep/source/js/back2top.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

KEEP.initBack2Top = () => {
2020-11-20 12:02:22 +08:00
KEEP.utils = {
2020-11-20 12:02:22 +08:00
...KEEP.utils,
back2BottomButton_dom: document.querySelector('.tool-scroll-to-bottom'),
back2top() {
const scrollTopTimer = setInterval(function () {
let top = document.body.scrollTop || document.documentElement.scrollTop;
let speed = top / 2;
if (document.body.scrollTop !== 0) {
document.body.scrollTop -= speed;
} else {
document.documentElement.scrollTop -= speed;
}
if (top === 0) {
clearInterval(scrollTopTimer);
}
2021-01-26 22:34:35 +08:00
}, 50);
},
back2Bottom() {
let scrollHeight = document.body.scrollHeight || document.documentElement.scrollHeight;
let scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
const scrollBottomTimer = setInterval(function () {
if (!scrollTop) scrollTop = 10;
scrollTop = Math.floor(scrollTop + scrollTop / 2);
window.scrollTo(0, scrollTop);
if (scrollTop >= scrollHeight) {
clearInterval(scrollBottomTimer);
}
}, 50);
},
initBack2Top() {
this.back2TopButton_dom.addEventListener('click', () => {
this.back2top();
});
},
initBack2Bottom() {
this.back2BottomButton_dom.addEventListener('click', () => {
this.back2Bottom();
});
},
}
KEEP.utils.initBack2Top();
KEEP.utils.initBack2Bottom();
}