2020-10-21 19:36:38 +08:00
|
|
|
window.addEventListener('DOMContentLoaded', () => {
|
2020-11-20 12:02:22 +08:00
|
|
|
KEEP.utils = {
|
2020-10-21 19:36:38 +08:00
|
|
|
|
2020-11-20 12:02:22 +08:00
|
|
|
...KEEP.utils,
|
2020-10-21 19:36:38 +08:00
|
|
|
|
|
|
|
back2TopButton_dom: document.querySelector('.scroll-to-top'),
|
2020-10-25 10:48:07 +08:00
|
|
|
back2BottomButton_dom: document.querySelector('.scroll-to-bottom'),
|
2020-10-21 19:36:38 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}, 30);
|
|
|
|
},
|
|
|
|
|
2020-10-25 10:48:07 +08:00
|
|
|
back2Bottom() {
|
|
|
|
let scrollHeight = document.body.scrollHeight || document.documentElement.scrollHeight;
|
2020-10-26 10:42:33 +08:00
|
|
|
let scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
|
2020-10-25 10:48:07 +08:00
|
|
|
const scrollBottomTimer = setInterval(function () {
|
|
|
|
if (!scrollTop) scrollTop = 10;
|
2020-10-26 10:42:33 +08:00
|
|
|
scrollTop = Math.floor(scrollTop + scrollTop / 2);
|
2020-10-25 10:48:07 +08:00
|
|
|
window.scrollTo(0, scrollTop);
|
|
|
|
if (scrollTop >= scrollHeight) {
|
|
|
|
clearInterval(scrollBottomTimer);
|
|
|
|
}
|
|
|
|
}, 50);
|
|
|
|
},
|
|
|
|
|
2020-10-21 19:36:38 +08:00
|
|
|
initBack2TopButton() {
|
2020-11-20 12:02:22 +08:00
|
|
|
if (KEEP.utils.back2TopButton_dom) {
|
|
|
|
KEEP.utils.back2TopButton_dom.addEventListener('click', () => {
|
2020-10-21 19:36:38 +08:00
|
|
|
this.back2top();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2020-10-25 10:48:07 +08:00
|
|
|
|
|
|
|
initBack2BottomButton() {
|
2020-11-20 12:02:22 +08:00
|
|
|
if (KEEP.utils.back2BottomButton_dom) {
|
|
|
|
KEEP.utils.back2BottomButton_dom.addEventListener('click', () => {
|
2020-10-25 10:48:07 +08:00
|
|
|
this.back2Bottom();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2020-10-21 19:36:38 +08:00
|
|
|
}
|
|
|
|
|
2020-11-20 12:02:22 +08:00
|
|
|
KEEP.utils.initBack2TopButton();
|
|
|
|
KEEP.utils.initBack2BottomButton();
|
2020-10-21 19:36:38 +08:00
|
|
|
});
|