Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a scroll to top btn #1332

Merged
merged 9 commits into from Jul 2, 2023
28 changes: 28 additions & 0 deletions src/pydata_sphinx_theme/assets/scripts/bootstrap.js
Expand Up @@ -21,8 +21,36 @@ function TriggerTooltip() {
});
}

/*******************************************************************************
* back to top button
*/
function backToTop() {
var btn = document.getElementById("pst-back-to-top");
btn.addEventListener("click", function () {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
});
}

function showBackToTop() {
var btn = document.getElementById("pst-back-to-top");
var header = document
.getElementsByClassName("bd-header")[0]
.getBoundingClientRect();
window.addEventListener("scroll", function () {
if (this.oldScroll > this.scrollY && this.scrollY > header.bottom) {
btn.style.display = "block";
} else {
btn.style.display = "none";
}
this.oldScroll = this.scrollY;
});
}

/*******************************************************************************
* Call functions after document loading.
*/

documentReady(TriggerTooltip);
documentReady(backToTop);
documentReady(showBackToTop);
13 changes: 13 additions & 0 deletions src/pydata_sphinx_theme/assets/styles/base/_base.scss
Expand Up @@ -177,3 +177,16 @@ pre {
padding-right: 10px;
}
}

// the back to top btn
#pst-back-to-top {
z-index: $zindex-tooltip;
position: fixed;
display: none;
top: 80vh;
left: 50vw;
transform: translate(-50%);
color: var(--pst-color-secondary-text);
background-color: var(--pst-color-secondary);
border: none;
}
9 changes: 9 additions & 0 deletions src/pydata_sphinx_theme/theme/pydata_sphinx_theme/layout.html
Expand Up @@ -52,6 +52,15 @@
{%- endblock %}

{%- block content %}
{# A tiny helper pixel to detect if we've scrolled #}
<div id="pst-scroll-pixel-helper"></div>

{# the scroll to top button #}
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
<i class="fa-solid fa-arrow-up"></i>
{{ _("Back to top") }}
</button>

{# checkbox to toggle primary sidebar #}
<input type="checkbox"
class="sidebar-toggle"
Expand Down