Skip to content

Commit

Permalink
refactor: improve JS/HTML for button back2top (#1054)
Browse files Browse the repository at this point in the history
The current "back to top" button is built by `<a>` tag, it would be more appropriate to replace it with the `<button>` tag.
  • Loading branch information
cotes2020 authored May 19, 2023
1 parent bef2ac0 commit f6bf6d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
15 changes: 9 additions & 6 deletions _javascript/modules/components/back-to-top.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
*/

export function back2top() {
$(window).on('scroll', () => {
if ($(window).scrollTop() > 50) {
$('#back-to-top').fadeIn();
const $window = $(window);
const $btn = $('#back-to-top');

$window.on('scroll', () => {
if ($window.scrollTop() > 50) {
$btn.fadeIn();
} else {
$('#back-to-top').fadeOut();
$btn.fadeOut();
}
});

$('#back-to-top').on('click', () => {
window.scrollTo(0, 0);
$btn.on('click', () => {
$window.scrollTop(0);
});
}
4 changes: 2 additions & 2 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@

<div id="mask"></div>

<a id="back-to-top" href="#" aria-label="back-to-top" class="btn btn-lg btn-box-shadow" role="button">
<button id="back-to-top" aria-label="back-to-top" class="btn btn-lg btn-box-shadow">
<i class="fas fa-angle-up"></i>
</a>
</button>

{% if site.pwa.enabled %}
<div
Expand Down

0 comments on commit f6bf6d0

Please sign in to comment.