Skip to content

Commit

Permalink
Add a "Back to top" button.
Browse files Browse the repository at this point in the history
Resolves #17.

Signed-off-by: Tao He <sighingnow@gmail.com>
  • Loading branch information
sighingnow committed Mar 12, 2022
1 parent adf2a11 commit 036963c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions _includes/footer.html
Expand Up @@ -12,4 +12,6 @@
<script src="{{site.baseurl}}/gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
<script src="{{site.baseurl}}/gitbook/gitbook-plugin-search-pro/search.js"></script>

<script src="{{site.baseurl}}/gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>

<script type="text/javascript" id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js"></script>
1 change: 1 addition & 0 deletions _includes/head.html
Expand Up @@ -23,6 +23,7 @@
<link rel="stylesheet" href="{{site.baseurl}}/gitbook/style.css">
<link rel="stylesheet" href="{{site.baseurl}}/gitbook/gitbook-plugin-fontsettings/website.css">
<link rel="stylesheet" href="{{site.baseurl}}/gitbook/gitbook-plugin-search-pro/search.css">
<link rel="stylesheet" href="{{site.baseurl}}/gitbook/gitbook-plugin-back-to-top-button/plugin.css">

<link rel="stylesheet" href="{{site.baseurl}}/gitbook/rouge/{{ site.syntax_highlighter_style }}.css">

Expand Down
55 changes: 55 additions & 0 deletions gitbook/gitbook-plugin-back-to-top-button/plugin.css
@@ -0,0 +1,55 @@
.back-to-top {
position: fixed;
bottom: 25px;
right: calc((100% - 300px - min(100% - 300px, 800px)) / 2 + 25px);
background: rgba(0, 0, 0, 0.5);
width: 50px;
height: 50px;
display: block;
text-decoration: none;
-webkit-border-radius: 35px;
-moz-border-radius: 35px;
border-radius: 35px;
display: none;
}
.back-to-top i {
color: #fff;
margin: 0;
position: relative;
left: 15px;
top: 14px;
font-size: 22px;
}
.back-to-top:hover {
background: rgba(0, 0, 0, 0.9);
cursor: pointer;
}
.book.color-theme-1 .back-to-top {
background: rgba(112, 66, 20, 0.5);
}
.book.color-theme-1 .back-to-top i {
color: #f3eacb;
}
.book.color-theme-1 .back-to-top:hover {
background: rgba(112, 66, 20, 0.9);
}
.book.color-theme-2 .back-to-top {
background: rgba(189, 202, 219, 0.5);
}
.book.color-theme-2 .back-to-top i {
color: #1C1F2B;
}
.book.color-theme-2 .back-to-top:hover {
background: rgba(189, 202, 219, 0.9);
}

@media only screen
and (min-device-width: 320px)
and (max-device-width: 480px)
and (-webkit-min-device-pixel-ratio: 2)
and (orientation: portrait) {
.back-to-top {
bottom: 10px;
right: 10px;
}
}
32 changes: 32 additions & 0 deletions gitbook/gitbook-plugin-back-to-top-button/plugin.js
@@ -0,0 +1,32 @@
require([
'gitbook',
'jquery'
], function(gitbook, $) {
gitbook.events.on('page.change', function() {
var back_to_top_button = ['<div class="back-to-top"><i class="fa fa-arrow-up"></i></div>'].join("");
$(".page-wrapper").append(back_to_top_button)

$(".back-to-top").hide();

$(".back-to-top").hover(function() {
$(this).css('cursor', 'pointer').attr('title', 'Back to top');
}, function() {
$(this).css('cursor', 'auto');
});

$('.book-body,.body-inner').on('scroll', function () {
if ($(this).scrollTop() > 100) {
$('.back-to-top').fadeIn();
} else {
$('.back-to-top').fadeOut();
}
});

$('.back-to-top').on('click', function () {
$('.book-body,.body-inner').animate({
scrollTop: 0
}, 800);
return false;
});
});
});

0 comments on commit 036963c

Please sign in to comment.