Skip to content

Commit 39a7226

Browse files
committed
Harden scroll-to-top button behavior
1 parent 7a92d42 commit 39a7226

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

_includes/footer.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% assign t = site.data.locales[page.lang][page.lang] %}
2-
<button id="scrollToTopButton" title="Go to top" aria-label="Scroll to top">
2+
<button id="scrollToTopButton" type="button" title="Scroll to top" aria-label="Scroll to top">
33
<svg aria-hidden="true" viewBox="0 0 24 24" focusable="false">
44
<path d="M12 4L5 11h4v9h6v-9h4z"></path>
55
</svg>

assets/css/button.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626
width: 20px;
2727
height: 26px;
2828
display: block;
29-
fill: #6a737d;
29+
fill: currentColor;
3030
}

assets/js/button.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11

2-
window.addEventListener('scroll', function () {
3-
var scrollToTopButton = document.getElementById('scrollToTopButton');
4-
if (window.scrollY > 300) {
5-
scrollToTopButton.style.display = 'flex';
6-
} else {
7-
scrollToTopButton.style.display = 'none';
8-
}
9-
});
10-
11-
document.getElementById('scrollToTopButton').addEventListener('click', function () {
12-
window.scrollTo({ top: 0, behavior: 'smooth' });
13-
});
2+
var scrollToTopButton = document.getElementById('scrollToTopButton');
3+
4+
if (scrollToTopButton) {
5+
var toggleScrollToTopButton = function () {
6+
if (window.scrollY > 300) {
7+
scrollToTopButton.style.display = 'flex';
8+
} else {
9+
scrollToTopButton.style.display = 'none';
10+
}
11+
};
12+
13+
window.addEventListener('scroll', toggleScrollToTopButton);
14+
15+
scrollToTopButton.addEventListener('click', function () {
16+
window.scrollTo({ top: 0, behavior: 'smooth' });
17+
});
18+
19+
toggleScrollToTopButton();
20+
}
1421

0 commit comments

Comments
 (0)