Scroll · 33 of 34

Text Fill Scrub

Text fills with colour in step with your scroll position rather than on a timer. Scroll progress drives background-size on a clipped gradient.

HTML · CSS · JavaScript · No dependencies · MIT

This one is driven by scroll position, so it reads best in context. See it on the scroll page.

HTML

<!-- Make the section tall so there is room to scrub through it -->
<section class="fill-scrub-section">
  <h2 class="fill-scrub-text">Filled by your scroll position, not by time</h2>
</section>

CSS

.fill-scrub-section {
  min-height: 60vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.fill-scrub-text {
  font-size: 2.5rem;
  max-width: 480px;
  text-align: center;
  /* Left half accent, right half faint — sliding the position fills the text */
  background: linear-gradient(90deg, #7c5cff 50%, rgba(255, 255, 255, 0.15) 50%);
  background-size: 200% 100%;
  background-position: 100% 0;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

JavaScript

var section = document.querySelector('.fill-scrub-section');
var text = document.querySelector('.fill-scrub-text');
var SPEED = 1.4; // >1 finishes the fill before the section fully passes

function tick() {
  var rect = section.getBoundingClientRect();
  var vh = window.innerHeight;
  // 0 when the section enters at the bottom, 1 once its center passes
  var progress = Math.max(0, Math.min(1,
    (vh - rect.top) / (vh + rect.height * 0.5) * SPEED));
  text.style.backgroundPosition = (100 - progress * 100) + '% 0';
  requestAnimationFrame(tick);
}
requestAnimationFrame(tick);