25 / blog / how-toJune 20267 min read

Pin sections on scroll without GSAP

The Apple product page. The Stripe feature walkthrough. The sticky image with scrolling text. These all use the same pattern: pin an element in place while the page scrolls past it. Here's how to do it without GSAP — in ~12 KB total.

  • How-To
  • scrollPin
  • svg-scroll-draw/pin
  • no GSAP
scrollPin alone
1.6 KB
Every API
11.9 KB
Callbacks
4 + progress
Licence
MIT
A viewport with a pinned image held 80 pixels below the top while copy scrolls past, above a scroll axis marking the pin zone and where each callback fires.viewportp = 0.5#panel · fixedtop: 80CopyscrollsscrollYpinDistance: 600onEnter ↓onLeaveBack ↑onProgressp 0 → 1onLeave ↓onEnterBack ↑
Fig. 1 — what scrollPin does: the viewport mid-pin, and where on the scroll each callback fires
01The idea

What “pin” means

A pinned element is fixed to a viewport position while the page scrolls a defined distance past it. When the scroll reaches the end of the “pin zone”, the element is released and continues with the page.

GSAP ScrollTrigger built the mental model: pin: true + a scroll distance. svg-scroll-draw ships the same pattern as scrollPin in the svg-scroll-draw/pin sub-path.

02Install

Install

terminal1 line
npm install svg-scroll-draw
03Minimum

The simplest pin

Pin #panel at the top of the viewport for one full viewport-height of scroll:

app.js8 lines
import { scrollPin } from 'svg-scroll-draw/pin';

const instance = scrollPin('#panel', {
  pinDistance: '100vh', // one screen, measured as CSS measures 100vh
});

// Later: remove pin and restore the DOM
// instance.destroy();

That's it. scrollPin wraps the target in a spacer div (so the page layout doesn’t jump), applies position: fixed when the element hits the viewport top, and releases it when the pin zone ends.

To feel a pin distance before you choose one, the scroll pin generator pins a feature tour in a box you scroll and copies the HTML, CSS and JS.

04Top

Pin at a custom viewport position

Use the top option to pin at a different viewport Y offset — e.g. 80px below the top (for a fixed navbar):

app.js4 lines
scrollPin('#sticky-image', {
  top:         80,           // pin 80px from viewport top
  pinDistance: 600,          // hold for 600px of scroll
});
05Callbacks

Lifecycle callbacks

Four callbacks mirror GSAP's ScrollTrigger API exactly:

app.js7 lines
scrollPin('#feature-panel', {
  pinDistance: 800,
  onEnter:     () => panel.classList.add('active'),
  onLeave:     () => panel.classList.remove('active'),
  onEnterBack: () => panel.classList.add('active'),
  onLeaveBack: () => panel.classList.remove('active'),
});
06onProgress

Progress callback

Use onProgress to drive another animation while the element is pinned:

app.js11 lines
import { scrollPin }    from 'svg-scroll-draw/pin';
import { scrollAnimate } from 'svg-scroll-draw';

// Fade in a caption as the user scrolls through the pin zone
scrollPin('#hero-image', {
  pinDistance: 600,
  onProgress: (p) => {
    // p is 0 → 1 through the pin zone
    caption.style.opacity = p.toFixed(3);
  },
});
07Pattern

Apple-style: pin image, scroll text

The classic pattern: image stays fixed while feature copy scrolls past it.

feature-section.js22 lines
import { scrollPin }    from 'svg-scroll-draw/pin';
import { scrollAnimate } from 'svg-scroll-draw';

// Pin the product image
const pin = scrollPin('#product-image', {
  top:         80,
  pinDistance: '300vh',
  onEnter: () => image.src = '/hero-active.png',
});

// Animate each feature block as it scrolls in
document.querySelectorAll('.feature-block')
  .forEach(el => {
    scrollAnimate(el, {
      props: {
        opacity:   [0, 1],
        transform: ['translateY(32px)', 'translateY(0)'],
      },
      easing: 'ease-out',
      once:   true,
    });
  });
08React

React usage

FeatureSection.tsx31 lines
'use client';
import { useEffect, useRef } from 'react';
import { scrollPin } from 'svg-scroll-draw/pin';

export function FeatureSection() {
  const imageRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    if (!imageRef.current) return;
    const instance = scrollPin(imageRef.current, {
      top:         80,
      pinDistance: '200vh',
      onEnter:     () => console.log('pinned'),
      onLeave:     () => console.log('released'),
    });
    return () => instance.destroy();
  }, []);

  return (
    <section className="flex gap-16">
      <div ref={imageRef} className="w-1/2">
        <img src="/product.png" alt="Product" />
      </div>
      <div className="w-1/2 space-y-64 py-32">
        <p>Feature one text...</p>
        <p>Feature two text...</p>
        <p>Feature three text...</p>
      </div>
    </section>
  );
}
09scrollSnap

Section snapping

Often pin sections are combined with scroll snap. svg-scroll-draw ships both:

app.js15 lines
import { scrollPin }  from 'svg-scroll-draw/pin';
import { scrollSnap } from 'svg-scroll-draw/snap';

// Snap between fullscreen sections
scrollSnap('.section', {
  duration:  600,
  easing:    'ease-in-out',
  onSnap:    (index) => console.log('section', index),
});

// Pin the nav while scrolling
scrollPin('#nav', {
  top:         0,
  pinDistance: document.body.scrollHeight,
});
10Destroy · refresh

Cleanup

Every scrollPin call returns an instance with destroy() and refresh(). Call destroy() in component cleanup / route change. Call refresh() if the page layout changes dynamically (accordion opens, dynamic content loads).

app.js9 lines
const pin = scrollPin('#panel', { pinDistance: 600 });

// Recalculate after layout change
document.querySelector('#accordion').addEventListener('click', () => {
  pin.refresh();
});

// Remove on page unload
window.addEventListener('beforeunload', () => pin.destroy());
11Reference

Full API

OptionTypeDescription
pinDistancenumber | '150vh'Scroll to stay pinned: pixels, which stay fixed, or CSS's vh, which follows rotation and window resizes but not a phone's address bar (2.20.0). Default: window.innerHeight
topnumberViewport Y (px) to pin at. Default: 0 (viewport top)
scrollContainerstring | ElementCustom scroll container. Default: window
onEnter() => voidFires when pin zone is entered (scrolling down)
onLeave() => voidFires when pin zone is exited at end (scrolling down)
onEnterBack() => voidFires when pin zone is re-entered (scrolling up)
onLeaveBack() => voidFires when pin zone is exited at start (scrolling up)
onProgress(p: number) => voidProgress 0–1 through the pin zone, every frame
→Parts list

Keep reading

01NextReplace GSAP ScrollTriggerThe full migration guide: fade, parallax, counters, text and video, side by side.02NextFull GSAP comparisonMeasured sizes, and where GSAP is still the better call.03NextAPI ReferenceEvery option of every API.04ExampleSticky pinned section on scrollPin an element while the page scrolls past it — the Apple and Stripe walkthrough pattern. Wrapper-based, no layout shift, full lifecycle callbacks.05ExampleVideo scrubbing on scrollScrub a video frame by frame with scroll position, the Apple product-page effect. scrollVideo binds currentTime to scroll in one call.06ExampleScroll image sequence (Apple-style product turn)Turn a product with the scroll, Apple-style: scrollFrames draws a numbered image sequence on a canvas in a pinned stage. Recipe with 71 real NASA frames.07ToolScroll pin generatorSet a pin distance, top and steps, scroll a feature tour pinned by scrollPin, and copy HTML, CSS, JS, React or a CSS-only version.08ToolScroll video scrub generatorA video that plays forwards and backwards with the scroll: pin it, set the window, range and easing, try your own file, copy the code.09Pagevs ScrollMagicScrollMagic pins too — but needs GSAP to animate anything.10PageScroll image sequence generatorThe pinned film as numbered stills on a canvas: turn a video into frames in the browser.11PageStacking cards on scroll generatorPinning with position: sticky alone: cards that each stop a step lower and pile up.

More from the blog