You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
var requestAnimationFrame =
|
|
window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
|
|
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
const goTopButton = document.querySelectorAll('#b2t')[0];
|
|
const windowViewPortHeight = window.innerHeight;
|
|
var isRequestingAnimationFrame = false;
|
|
|
|
if (!goTopButton) {
|
|
return;
|
|
}
|
|
|
|
goTopButton.addEventListener('click', function () {
|
|
window.scrollTo({
|
|
top: 0,
|
|
behavior: 'smooth',
|
|
});
|
|
});
|
|
|
|
window.addEventListener('scroll', function () {
|
|
if (!isRequestingAnimationFrame) {
|
|
requestAnimationFrame(filterGoTopButtonVisibility);
|
|
isRequestingAnimationFrame = true;
|
|
}
|
|
});
|
|
|
|
function filterGoTopButtonVisibility(timestamp) {
|
|
var windowPageYOffset = window.pageYOffset || document.documentElement.scrollTop;
|
|
if (windowPageYOffset > windowViewPortHeight) {
|
|
goTopButton.style.display = 'block';
|
|
isRequestingAnimationFrame = false;
|
|
} else {
|
|
goTopButton.style.display = 'none';
|
|
requestAnimationFrame(filterGoTopButtonVisibility);
|
|
}
|
|
}
|
|
});
|