Get access to a growing collection of Premium Ghost Themes with the Yearly Subscription 🔥33% Off

How to Add a Scroll Top & Progress Indicator Button in Ghost

Tutorial to add a scroll to top button to your Ghost theme, to give users thee option to quickly scroll back to the top. Enhance it to work as a progress indicator at the same time.

Norbert4 min read

When your page is very long you may want to give your users an option to quickly scroll back to the top of the page. In such cases, the best solution is to add a button that will appear after the user scrolls a specified number of pixels.

These buttons usually float in the bottom corner of sites and then take you back to the top of the page when clicked. They are pretty easy to create with JavaScript, we just have to look out to make it non-obtrusive while still being a large enough target to tap or click.

We will start simple, then extend it with additional functionality, like tracking the scroll progress and indicating it on the button.

The simple scroll-top button

Here is the button markup with an arrow pointing up inside:

  class="btn-toggle-round scroll-top js-scroll-top"
  title="Scroll to top"
    class="icon icon-tabler icon-tabler-arrow-up"
    viewBox="0 0 24 24"
    <path stroke="none" d="M0 0h24v24H0z" fill="none" />
    <line x1="12" y1="5" x2="12" y2="19" />
    <line x1="18" y1="11" x2="12" y2="5" />
    <line x1="6" y1="11" x2="12" y2="5" />

Styling the button:

.scroll-top {
  position: fixed;
  z-index: 50;
  padding: 0;
  right: 20px;
  bottom: 20px;
  opacity: 1;
  visibility: visible;
  transform: translateY(0px);
  height: 46px;
  width: 46px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.4s ease;
  border: none;
  box-shadow: inset 0 0 0 2px #ccc;
  color: #ccc;
  background-color: #fff;

.scroll-top .icon-tabler-arrow-up {
  position: absolute;
  stroke-width: 2px;
  stroke: #333;

.scroll-top:hover {
  color: var(--ghost-accent-color);

.scroll-top:hover .icon-tabler-arrow-up {
  stroke: var(--ghost-accent-color);

The above CSS will make the scroll top button fixed at the bottom right corner of the page, you can easily adjust the position, colors, or anything else.

Now, to make the button functional, we need a bit of javascript, which can be added to the button ( onclick event) or separately from code injection:

const scrollTopBtn = document.querySelector('.js-scroll-top');
if (scrollTopBtn) {
  scrollTopBtn.onclick = () => {
    window.scrollTo({ top: 0, behavior: 'smooth' });

We have the basic functionality and styling, now let's see how we can improve it.

Animating the button visibility

You probably wouldn't want the button to be visible by default, as when you are at the top of the page, you don't need to scroll up. So let's improve this and make the bottom visible only after a certain amount of scroll.

For this we need to track the scroll event and change the scroll top button:

const offset = 100;
  function (event) {
    //Scroll back to top when offset is met
    const scrollPos =
      window.scrollY ||
      window.scrollTop ||
    scrollPos > offset
      ? scrollTop.classList.add('is-active')
      : scrollTop.classList.remove('is-active');

This will add the .is-active class to the button, which we can use to modify the style and animate the button:

.scroll-top {
  opacity: 0;
  visibility: hidden;
  transform: translateY(15px);
} {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);

Great, now let's see what else we can improve. An interesting and useful feature we can add is to make the button a progress indicator at the same time.

Add progress indicator function

To achieve this, we need to extend the markup and add the following inside the <button>:

<svg class="progress-circle" width="100%" height="100%" viewBox="-1 -1 102 102">
  <path d="M50,1 a49,49 0 0,1 0,98 a49,49 0 0,1 0,-98" />

We'll use this progress circle SVG to highlight the scroll status, for this we need to calculate the percent of the scroll using javascript:

const progressPath = document.querySelector('.scroll-top path');
const pathLength = progressPath.getTotalLength(); = = 'none'; = `${pathLength} ${pathLength}`; = pathLength;
progressPath.getBoundingClientRect(); = = 'stroke-dashoffset 10ms linear';
const updateProgress = function() {
  const scroll = window.scrollY || window.scrollTopBtn || document.documentElement.scrollTopBtn;

  const docHeight = Math.max(
    document.body.scrollHeight, document.documentElement.scrollHeight,
    document.body.offsetHeight, document.documentElement.offsetHeight,
    document.body.clientHeight, document.documentElement.clientHeight

  const windowHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);

  const height = docHeight - windowHeight;
  var progress = pathLength - (scroll * pathLength / height); = progress;

window.addEventListener('scroll', function(event) {

Of course, some additional CSS is needed to make all of this work:

.scroll-top svg path {
  fill: none;

.scroll-top svg.progress-circle path {
  stroke: #333;
  stroke-width: 4;
  transition: all 0.4s ease;

.scroll-top:hover .progress-circle path {
  stroke: var(--ghost-accent-color);

Now we have everything, a scroll top function that also acts as a progress indicator. Check out the whole code, and see it in action:

More in Guides & Tutorials

All guides
Ghost Pro Hosting

Get the best managed Ghost CMS hosting and focus on bringing value to your audience.