Skip to content

Commit

Permalink
Merge pull request #794 from h-enk/back-to-top-button
Browse files Browse the repository at this point in the history
Add to top button as an option
  • Loading branch information
h-enk committed Jul 5, 2022
2 parents 27d3c24 + 9f67dd0 commit 6141948
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
20 changes: 20 additions & 0 deletions assets/js/to-top.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var topbutton = document.getElementById('toTop');

if (topbutton !== null) {

topbutton.style.display = 'none';
window.onscroll = function() {
scrollFunction()
};

}

function scrollFunction() {

if (document.body.scrollTop > 40 || document.documentElement.scrollTop > 40) {
topbutton.style.display = 'block';
} else {
topbutton.style.display = 'none';
}

}
1 change: 1 addition & 0 deletions config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ lastMod = false
docsVersioning = false
fullWidth = false
navbarSticky = true
toTopButton = false

[menu]
[menu.section]
Expand Down
5 changes: 5 additions & 0 deletions layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@
{{ block "sidebar-footer" . }}{{ end }}
{{ partial "footer/footer.html" . }}
{{ partial "footer/script-footer.html" . }}
{{ if eq .Site.Params.options.toTopButton true -}}
<div class="d-flex fixed-bottom pb-4 pb-lg-5 pe-4 pe-lg-5">
<a id="toTop" href="#" class="btn btn-outline-primary rounded-circle ms-auto p-2"><span class="visually-hidden">Top</span><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-chevron-up"><polyline points="18 15 12 9 6 15"></polyline></svg></a>
</div>
{{ end }}
</body>
</html>
6 changes: 6 additions & 0 deletions layouts/partials/footer/script-footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
{{ $scrollLock := resources.Get "js/scroll-lock.js" | js.Build -}}
{{ $slice = $slice | append $scrollLock -}}

{{ if .Site.Params.options.toTopButton -}}
{{ $toTopButton := resources.Get "js/to-top.js" -}}
{{ $toTopButton := $toTopButton | js.Build -}}
{{ $slice = $slice | append $toTopButton -}}
{{ end -}}

{{ $js := $slice | resources.Concat "main.js" -}}

{{ if eq (hugo.Environment) "development" -}}
Expand Down

0 comments on commit 6141948

Please sign in to comment.