Skip to content

Commit 54311e5

Browse files
12rambautrallard
andauthored
feat: add a scroll to top btn (#1332)
* feat: add a back to top btn * style: improve back to top btn * style: use on background instead * Update src/pydata_sphinx_theme/theme/pydata_sphinx_theme/layout.html Co-authored-by: Tania Allard <[email protected]> * Update src/pydata_sphinx_theme/theme/pydata_sphinx_theme/layout.html Co-authored-by: Tania Allard <[email protected]> * Update src/pydata_sphinx_theme/assets/styles/base/_base.scss * Update src/pydata_sphinx_theme/assets/styles/base/_base.scss * fix: move btn to the bottom and make it blue * fix: use secondary colors --------- Co-authored-by: Tania Allard <[email protected]>
1 parent bb19e7d commit 54311e5

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/pydata_sphinx_theme/assets/scripts/bootstrap.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,36 @@ function TriggerTooltip() {
2121
});
2222
}
2323

24+
/*******************************************************************************
25+
* back to top button
26+
*/
27+
function backToTop() {
28+
var btn = document.getElementById("pst-back-to-top");
29+
btn.addEventListener("click", function () {
30+
document.body.scrollTop = 0;
31+
document.documentElement.scrollTop = 0;
32+
});
33+
}
34+
35+
function showBackToTop() {
36+
var btn = document.getElementById("pst-back-to-top");
37+
var header = document
38+
.getElementsByClassName("bd-header")[0]
39+
.getBoundingClientRect();
40+
window.addEventListener("scroll", function () {
41+
if (this.oldScroll > this.scrollY && this.scrollY > header.bottom) {
42+
btn.style.display = "block";
43+
} else {
44+
btn.style.display = "none";
45+
}
46+
this.oldScroll = this.scrollY;
47+
});
48+
}
49+
2450
/*******************************************************************************
2551
* Call functions after document loading.
2652
*/
2753

2854
documentReady(TriggerTooltip);
55+
documentReady(backToTop);
56+
documentReady(showBackToTop);

src/pydata_sphinx_theme/assets/styles/base/_base.scss

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,16 @@ pre {
166166
padding-right: 10px;
167167
}
168168
}
169+
170+
// the back to top btn
171+
#pst-back-to-top {
172+
z-index: $zindex-tooltip;
173+
position: fixed;
174+
display: none;
175+
top: 80vh;
176+
left: 50vw;
177+
transform: translate(-50%);
178+
color: var(--pst-color-secondary-text);
179+
background-color: var(--pst-color-secondary);
180+
border: none;
181+
}

src/pydata_sphinx_theme/theme/pydata_sphinx_theme/layout.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@
5252
{%- endblock %}
5353

5454
{%- block content %}
55+
{# A tiny helper pixel to detect if we've scrolled #}
56+
<div id="pst-scroll-pixel-helper"></div>
57+
58+
{# the scroll to top button #}
59+
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
60+
<i class="fa-solid fa-arrow-up"></i>
61+
{{ _("Back to top") }}
62+
</button>
63+
5564
{# checkbox to toggle primary sidebar #}
5665
<input type="checkbox"
5766
class="sidebar-toggle"

0 commit comments

Comments
 (0)