Scroll · 34 of 34

Zoom Through

The headline scales up toward you as you scroll past, as if you are moving through it. Scroll progress maps to a scale and opacity, applied each frame.

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="zoom-through-section">
  <h2 class="zoom-through-text">Closer</h2>
</section>

CSS

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

.zoom-through-text {
  font-size: 2.5rem;
  color: #f0f0f5;
  transform-origin: center center;
  will-change: transform, opacity;
}

JavaScript

var section = document.querySelector('.zoom-through-section');
var text = document.querySelector('.zoom-through-text');
var MIN_SCALE = 0.6;
var MAX_GROWTH = 1.4;   // scale reaches MIN_SCALE + MAX_GROWTH at full progress
var FADE_START = 0.75;  // progress where the fade-out begins

function tick() {
  var rect = section.getBoundingClientRect();
  var vh = window.innerHeight;
  // 0 when the section enters at the bottom, 1 when it leaves at the top
  var progress = Math.max(0, Math.min(1, (vh - rect.top) / (vh + rect.height)));
  var scale = MIN_SCALE + progress * MAX_GROWTH;
  var opacity = progress < FADE_START
    ? 1
    : Math.max(0, 1 - (progress - FADE_START) * 4);
  text.style.transform = 'scale(' + scale + ')';
  text.style.opacity = opacity;
  requestAnimationFrame(tick);
}
requestAnimationFrame(tick);