backtotop

This commit is contained in:
troy
2022-02-14 12:23:22 +01:00
parent 8170b81554
commit d318faca2c
7 changed files with 104 additions and 7 deletions

31
backtotop.js Normal file
View File

@@ -0,0 +1,31 @@
(function() {
'use strict';
function trackScroll() {
var scrolled = window.pageYOffset;
var coords = document.documentElement.clientHeight;
console.log(coords,scrolled);
if (scrolled > coords) {
goTopBtn.classList.add('back_to_top-show');
}
if (scrolled < coords) {
goTopBtn.classList.remove('back_to_top-show');
}
}
function backToTop() {
if (window.pageYOffset > 0) {
window.scrollBy(0, -30);
setTimeout(backToTop, 0);
}
}
var goTopBtn = document.createElement("div");
goTopBtn.id = 'back_to_top';
goTopBtn.innerHTML = 'UP';
document.body.appendChild(goTopBtn);
window.addEventListener('scroll', trackScroll);
goTopBtn.addEventListener('click', backToTop);
})();