Skip to content

Commit

Permalink
add back to top button #3
Browse files Browse the repository at this point in the history
  • Loading branch information
matsuyoshi30 committed Mar 16, 2020
1 parent 24131d6 commit 0f2d1a8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -101,6 +101,10 @@ title: "Search"

If you want to use TableOfContent, you need to write words greater than 400, and set `true` frontmatter `toc`.

### Back To Top Button

If you want to use Back To Top Button, you need to write words greater than 400, and set `true` frontmatter `backtotop`.

## Frontmatter example

```
Expand All @@ -110,6 +114,7 @@ date = 2020-02-15T20:00:00+09:00
tags = ["tags here"]
draft = false
toc = false
backtotop = false
+++
# Title
Expand Down
1 change: 1 addition & 0 deletions archetypes/default.md
Expand Up @@ -4,6 +4,7 @@ date = {{ .Date }}
tags = [""]
draft = false
toc = false
backtotop = false
+++

# Title
Expand Down
1 change: 1 addition & 0 deletions layouts/_default/baseof.html
Expand Up @@ -9,4 +9,5 @@
{{ partial "footer.html" . }}
</body>
{{ partial "darkmode.html" . }}
{{ partial "backtotop.html" . }}
</html>
5 changes: 5 additions & 0 deletions layouts/_default/single.html
Expand Up @@ -11,5 +11,10 @@
</div>
{{ end }}
</article>
{{ if and (gt .WordCount 400) (.Param "backtotop") }}
<button onclick="topFunction()" id="backtotopButton">
<i class="fa fa-angle-up"></i>
</button>
{{ end }}
</div>
{{ end }}
16 changes: 16 additions & 0 deletions layouts/partials/backtotop.html
@@ -0,0 +1,16 @@
<script>
document.addEventListener('scroll', function() {
if (document.body.scrollTop > 50 || document.documentElement.scrollTop > 50) {
document.getElementById('backtotopButton').style.opacity = "1";
document.getElementById('backtotopButton').style.transition = "0.5s";
} else {
document.getElementById('backtotopButton').style.opacity = "0";
document.getElementById('backtotopButton').style.transition = "0.5s";
}
});

function topFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
</script>
19 changes: 19 additions & 0 deletions static/css/main.css
Expand Up @@ -293,6 +293,25 @@ code {
border-radius: 4px;
}

#backtotopButton {
position: fixed;
bottom: 20px;
right: 20px;
z-index: 99;
border: none;
outline: none;
background-color: #eeeeff;
cursor: pointer;
padding: 15px;
border-radius: 10px;
font-size: 16px;
text-align: center;
}

#backtotopButton:hover {
background-color: #aaaaaa;
}

.searchBoxContainer {
position: relative;
width: 300px;
Expand Down

0 comments on commit 0f2d1a8

Please sign in to comment.