Skip to content

Commit ee6b13d

Browse files
committed
Add back to top button
Uses theme colors and border radius from search-box, otherwise copy paste from: https://www.w3schools.com/howto/howto_js_scroll_to_top.asp
1 parent 17076d0 commit ee6b13d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

sass/site.scss

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,27 @@
1313
@import 'post-list';
1414

1515
body {
16-
padding-top: $header-height;
16+
padding-top: $header-height;
17+
18+
#back-to-top-button {
19+
display: none;
20+
position: fixed;
21+
bottom: 20px;
22+
right: 30px;
23+
z-index: 99;
24+
border: none;
25+
outline: none;
26+
background-color: $color-base-primary;
27+
color: $color-text-inverted;
28+
cursor: pointer;
29+
padding: 15px;
30+
border-radius: 2px;
31+
font-size: 18px;
32+
33+
&:hover {
34+
background-color: $color-base-hover;
35+
}
36+
}
1737
}
1838

1939
footer {

static/back_to_top.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Get the button:
2+
let mybutton = document.getElementById("back-to-top-button");
3+
4+
// When the user scrolls down 20px from the top of the document, show the button
5+
window.onscroll = function() {scrollFunction()};
6+
7+
function scrollFunction() {
8+
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
9+
mybutton.style.display = "block";
10+
} else {
11+
mybutton.style.display = "none";
12+
}
13+
}
14+
15+
// When the user clicks on the button, scroll to the top of the document
16+
function backToTop() {
17+
document.body.scrollTop = 0; // For Safari
18+
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
19+
}

templates/base.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@
2929
<body>
3030
{% block body %} {% endblock %}
3131

32+
<button onclick="backToTop()" id="back-to-top-button" title="Go to top">Top</button>
33+
3234
<script type="text/javascript" src="{{ get_url(path='elasticlunr.min.js') }}" defer></script>
3335
<script type="text/javascript" src="{{ get_url(path='api_search.js') }}" defer></script>
3436
<script type="text/javascript" src="{{ get_url(path='search.js') }}" defer></script>
37+
<script type="text/javascript" src="{{ get_url(path='back_to_top.js') }}" defer></script>
3538
</body>
3639

3740
</html>

0 commit comments

Comments
 (0)