Skip to content

Commit

Permalink
feat: add a scroll to top btn (#1332)
Browse files Browse the repository at this point in the history
* 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 <taniar.allard@gmail.com>

* Update src/pydata_sphinx_theme/theme/pydata_sphinx_theme/layout.html

Co-authored-by: Tania Allard <taniar.allard@gmail.com>

* 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 <taniar.allard@gmail.com>
  • Loading branch information
12rambau and trallard committed Jul 2, 2023
1 parent bb19e7d commit 54311e5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/pydata_sphinx_theme/assets/scripts/bootstrap.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,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
Original file line number Diff line number Diff line change
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

0 comments on commit 54311e5

Please sign in to comment.