Creating a Custom “Back to Top” Button That Appears on Scroll (Shopify Tutorial)

Creating a Custom “Back to Top” Button That Appears on Scroll (Shopify Tutorial)

In this tutorial, you’ll learn how to build a lightweight, fully custom “Back to Top” button for a Shopify store. The button remains hidden initially, appears after the user scrolls, and smoothly returns the visitor to the top of the page. This solution is app-free, performance-friendly, and compatible with all Shopify OS 2.0 themes.

Overview

Long product pages and collection layouts often require users to scroll extensively. A “Back to Top” button improves usability by providing quick navigation, especially on mobile and content-heavy pages.

  • No third-party apps required
  • Minimal JavaScript
  • Smooth scrolling animation
  • Responsive and accessible

Step 1: Add the HTML Markup

Start by adding the button markup to your theme. For best results, place it in footer.liquid or create a reusable snippet.

<button
  id="backToTop"
  class="back-to-top"
  aria-label="Back to top"
>
  ↑
</button>

This button is hidden by default and will be controlled via CSS and JavaScript.

Step 2: Apply Styling with CSS

Add the following styles to your theme’s main stylesheet (base.css, theme.css, or custom.css).

.back-to-top {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background-color: #111;
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transform: translateY(10px);
  transition: opacity 0.3s ease, transform 0.3s ease, visibility 0.3s ease;
  z-index: 999;
}

.back-to-top.is-visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.back-to-top:hover {
  background-color: #000;
}

The .is-visible class will be toggled dynamically when the user scrolls.

Step 3: Add Scroll and Click Behavior

Next, add the JavaScript responsible for detecting scroll position and enabling smooth scrolling back to the top. Insert this script into theme.js or a global JavaScript file.

document.addEventListener("DOMContentLoaded", function () {
  const backToTopButton = document.getElementById("backToTop");

  if (!backToTopButton) return;

  window.addEventListener("scroll", function () {
    if (window.scrollY > 300) {
      backToTopButton.classList.add("is-visible");
    } else {
      backToTopButton.classList.remove("is-visible");
    }
  });

  backToTopButton.addEventListener("click", function () {
    window.scrollTo({
      top: 0,
      behavior: "smooth"
    });
  });
});

Step 4: Mobile Optimization

To ensure the button remains unobtrusive on smaller screens, add the following responsive styles.

@media (max-width: 768px) {
  .back-to-top {
    width: 44px;
    height: 44px;
    right: 16px;
    bottom: 16px;
  }
}

Final Result

Once implemented, your Shopify store will include a modern, user-friendly “Back to Top” button that:

  • Appears only after scrolling
  • Uses smooth scroll animation
  • Works across desktop and mobile devices
  • Has no impact on page performance

Recommended Enhancements

For advanced implementations, consider extending this feature with:

  • Theme Editor toggle using section settings
  • Dynamic color matching with theme color schemes
  • Scroll progress indicator
  • SVG icon with subtle animation

This approach follows Shopify best practices and works seamlessly with OS 2.0 themes, including Dawn-based templates.

Back to blog

Leave a comment