How to Create a Circular SVG Scroll Progress Indicator & Smooth Back-to-Top Button
A circular scroll indicator calculates scroll ratio as `window.scrollY / (document.documentElement.scrollHeight - window.innerHeight)` and updates an SVG circle's `stroke-dashoffset` (`circumference * (1 - ratio)`). Clicking the button invokes `window.scrollTo({ top: 0, behavior: "smooth" })`.
Step 1: Construct the Circular SVG Button Markup
Build the circular button container containing background ring, animated progress ring, and up-arrow icon.
<button class="scroll-top-btn" id="scrollTopBtn" aria-label="Scroll back to top of page">
<svg class="progress-ring" viewBox="0 0 48 48">
<circle class="ring-track" cx="24" cy="24" r="20"></circle>
<circle class="ring-fill" id="progressCircle" cx="24" cy="24" r="20"></circle>
</svg>
<span class="arrow-icon">↑</span>
</button>
Step 2: Apply Circle Dashoffset & Visibility Styling
Rotate the ring -90 degrees so progress begins at 12 o'clock, and fade in smoothly upon scroll.
.scroll-top-btn {
position: fixed;
bottom: 24px;
right: 24px;
width: 48px;
height: 48px;
border-radius: 50%;
border: none;
background: #0f172a;
color: #fff;
cursor: pointer;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
opacity: 0;
transform: translateY(16px);
pointer-events: none;
transition: opacity 0.3s ease, transform 0.3s ease;
z-index: 950;
}
.scroll-top-btn.is-visible {
opacity: 1;
transform: translateY(0);
pointer-events: auto;
}
.progress-ring {
position: absolute;
inset: 0;
transform: rotate(-90deg);
}
.ring-track {
fill: none;
stroke: rgba(255, 255, 255, 0.1);
stroke-width: 3;
}
.ring-fill {
fill: none;
stroke: #6366f1;
stroke-width: 3;
stroke-linecap: round;
stroke-dasharray: 125.66;
stroke-dashoffset: 125.66;
transition: stroke-dashoffset 0.1s linear;
}
.arrow-icon {
position: relative;
font-size: 1.2rem;
}
Step 3: Implement the Scroll Progress Calculation
Compute scroll ratio, update stroke-dashoffset, and attach smooth scroll to top listener.
const btn = document.getElementById('scrollTopBtn');
const circle = document.getElementById('progressCircle');
const CIRCUMFERENCE = 2 * Math.PI * 20; // ~125.66
function onScroll() {
const scrollTop = window.scrollY || document.documentElement.scrollTop;
const docHeight = document.documentElement.scrollHeight - window.innerHeight;
const progress = Math.min(1, Math.max(0, scrollTop / (docHeight || 1)));
circle.style.strokeDashoffset = CIRCUMFERENCE * (1 - progress);
btn.classList.toggle('is-visible', scrollTop > 300);
}
window.addEventListener('scroll', () => {
requestAnimationFrame(onScroll);
});
btn.addEventListener('click', () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
onScroll();
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
Here's my golden rule when implementing this in production: test across multiple devices and browsers early. Keep your DOM hierarchy shallow, optimize assets with modern formats (like WebP/AVIF), and verify keyboard navigation for accessibility.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
Why does stroke-dashoffset = circumference * (1 - progress) work?
The stroke-dasharray creates a dash pattern equal to the full perimeter (125.66px). Offsetting the dash by 100% hides the line completely; shifting the offset towards 0 reveals the line progressively.
Why use document.documentElement.scrollHeight - window.innerHeight?
Scroll height includes the entire page length. When scrolled to the very bottom, `scrollY` equals `scrollHeight - innerHeight`. Subtracting `innerHeight` ensures progress reaches 100% at the page footer.
Does smooth scrolling work on all browsers?
Yes, `window.scrollTo({ top: 0, behavior: "smooth" })` has over 97% global support across Chrome, Edge, Safari, and Firefox.
Why should the button remain hidden until the user scrolls past 300px?
Showing a back-to-top button at the very top of the page adds visual clutter without any practical utility. Fading in after 300px provides assistance only when needed.
How do you prevent the button from overlapping mobile floating navbars or chat widgets?
Offset the bottom coordinate using safe-area-insets: `bottom: calc(24px + env(safe-area-inset-bottom, 0px));` or elevate above other widgets with z-index.