Skip to content

Commit

Permalink
增加了“返回顶部”按钮,同时toc容器y如果overflow,滚动条会视情况生成,不会默认全生成了。
Browse files Browse the repository at this point in the history
  • Loading branch information
Neboer committed Sep 20, 2024
1 parent 5174beb commit c9c254b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
34 changes: 33 additions & 1 deletion themes/nboat/layout/post.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
</div>
</main>

<script>
<button type="button" class="btn btn-danger btn-floating btn-lg" id="btn-back-to-top">
<i class="fas fa-arrow-up"></i>
</button>

<script>
// auto scroll to active toc item
document.addEventListener('DOMContentLoaded', function () {
var firstScrollSpyEl = document.querySelector('[data-bs-spy="scroll"]')
let scroll_timer;
Expand All @@ -45,4 +49,32 @@
});
});
// back to top button
// code from https://mdbootstrap.com/docs/standard/extended/back-to-top/
//Get the button
let mybutton = document.getElementById("btn-back-to-top");
// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function () {
scrollFunction();
};
function scrollFunction() {
if (
document.body.scrollTop > 20 ||
document.documentElement.scrollTop > 20
) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
mybutton.addEventListener("click", backToTop);
function backToTop() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
</script>
9 changes: 8 additions & 1 deletion themes/nboat/source/css/partial/post.styl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,14 @@
height: 80vh
position sticky
top 2rem
overflow-y: scroll
overflow-y: auto

#btn-back-to-top
position: fixed;
bottom: 20px;
right: 20px;
display: none;


@media (max-width grid-breakpoints-md)
.post-details
Expand Down

0 comments on commit c9c254b

Please sign in to comment.