web/_javascript/modules/components/back-to-top.js
Cotes Chung f6bf6d0864
refactor: improve JS/HTML for button back2top (#1054)
The current "back to top" button is built by `<a>` tag, it would be more appropriate to replace it with the `<button>` tag.
2023-05-20 01:07:18 +08:00

20 lines
370 B
JavaScript

/**
* Reference: https://bootsnipp.com/snippets/featured/link-to-top-page
*/
export function back2top() {
const $window = $(window);
const $btn = $('#back-to-top');
$window.on('scroll', () => {
if ($window.scrollTop() > 50) {
$btn.fadeIn();
} else {
$btn.fadeOut();
}
});
$btn.on('click', () => {
$window.scrollTop(0);
});
}