Hugo + js = back to top button
Example on how to add a back to top button. This has nothing to do with Hugo itself, it is just a minimal Javascript snipped that allows you to jump back to the top of the page.
This article is a fork of the back-to-top feature from Hugo meme theme, all credit goes to reuixiy.
This article was written for Hugo 0.70
.
Code
The code is completely optional, it will work with just HTML as well. The Javascript will automatically show/hide the back to top button depending on how far the page is scrolled down. In addition a throttle
function is added on scroll so the page goes smoothly to the top instead of instantly “teleporting”. If you have many scroll
eventhandlers you might want to leave out the throttle
function for better performance.
|
|
In HTML add a back to top button, anywhere usually at the bottom of the viewport.
<div id="back-to-top">
<a href="#">{{ partial "inline-svg" .Site.Params.content.backToTopIcon }}</a>
</div>
Finally the SCSS.
|
|
- Permalink: //oostens.me/posts/hugo--js-back-to-top-button/
- License: The text and content is licensed under CC BY-NC-SA 4.0. All source code I wrote on this page is licensed under The Unlicense; do as you please, I'm not liable nor provide warranty.