Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                  <header class="site-header">
    <h1 class="sr-only">Scrollnapping animations</h1>

    <!-- Because Chrome crashes if an overflow is set on the fieldset while a named timeline is on a child element -->
    <!-- ¯\_(ツ)_/¯ -->
    <div class="fieldset-wrapper">
      <fieldset>
        <legend class="sr-only">Effects</legend>

        <!-- radio buttons for each of the @keyframes above -->
        <input type="radio" id="blink-effect" name="effect" value="blink" checked class="sr-only">
        <label for="blink-effect">Blink</label>

        <input type="radio" id="horizontal-scroll-effect" name="effect" value="horizontal-scroll" class="sr-only">
        <label for="horizontal-scroll-effect">Horizontal scroll</label>

        <input type="radio" id="backwards-scroll-effect" name="effect" value="backwards-scroll" class="sr-only">
        <label for="backwards-scroll-effect">Backwards scroll</label>

        <input type="radio" id="zoom-scroll-effect" name="effect" value="zoom-scroll" class="sr-only">
        <label for="zoom-scroll-effect">Zoom scroll</label>
      </fieldset>
    </div>

    <nav>
      <ul class="indicator">
        <li><a href="#snapping"><span class="sr-only">Snapping</span></a></li>
        <li><a href="#scrolling"><span class="sr-only">Scrolling</span></a></li>
        <li><a href="#layout"><span class="sr-only">Layout</span></a></li>
        <li><a href="#transition"><span class="sr-only">Transition</span></a></li>
        <li><a href="#caveats"><span class="sr-only">Caveats</span></a></li>
      </ul>
    </nav>
  </header>
  <main>
    <section id="snapping" class="section">
      <div class="content">
        <h2><strong>First</strong>, we set up the <em>snapping</em> points</h2>

        <div class="text">
          <img src="https://assets.codepen.io/197359/flower-white.png" alt="">
          <p>We set the scrolling element, in this case our <code class="selector">HTML</code> element, to forcibly snap to the Y axis by using <code class="property">scroll-snap-type: y mandatory</code>.</p>

          <p>And then we set <code class="selector">section</code> as the snapping elements by using <code class="property">scroll-snap-align: start</code>.</p>
        </div>
      </div>
    </section>
    <section id="scrolling" class="section">
      <div class="content">
        <h2><strong>Next</strong>, we set up the <em>scrolling</em> animation</h2>

        <div class="text">
          <img src="https://assets.codepen.io/197359/flower-yellow.png" alt="">
          <p>We track the <code class="property">view()</code> position of the <code class="selector">section</code> elements using the named timeline <code class="property">view-timeline: --section;</code>. We had previously set the <code class="property">timeline-scope: --section</code> up in our <code class="selector">HTML</code> element, so we can access it from anywhere in the document.</p>

          <p>We animate the <code class="selector">.content</code> children using <code class="property">animation-timeline: --section;</code>. The <code class="selector">.content</code> element will now animate based on its parent <code class="selector">section</code>'s position. This is important due to how we're handling the layout in the next section.</p>
        </div>
      </div>
    </section>
    <section id="layout" class="section">
      <div class="content">
        <h2><strong>Then</strong>, we position a <em>fixed</em> layout</h2>

        <div class="text">
          <img src="https://assets.codepen.io/197359/flower-blue.png" alt="">
          <p>We set the <code class="selector">.content</code> elements to <code class="property">position: fixed</code>, so they're removed from the normal document flow and stack on top of each other, giving them a solid background, so only one is visible at a time.</p>

          <p>Their parent <code class="selector">section</code>s are positioned as normal in the layer below, taking up space, scroll-snapping, and powering the <code class="property">animation-timeline</code>.</p>
        </div>
      </div>
    </section>
    <section id="transition" class="section">
      <div class="content">
        <h2><strong>Finally</strong>, we create the <em>transition</em> effects</h2>

        <div class="text">
          <img src="https://assets.codepen.io/197359/flower-red.png" alt="">
          <p>By setting the <code class="selector">.content</code> elements to <code class="property">position: fixed</code>, we can now transition between them without a visible scrolling movement.</p>

          <p>We create a normal <code class="selector">@keyframe</code> animation to our liking to transition between them. Check the navigation menu to see different effects.</p>
        </div>
      </div>
    </section>
    <section id="caveats" class="section">
      <div class="content">
        <h2><strong>Caveats</strong></h2>

        <div class="text">
          <img src="https://assets.codepen.io/197359/flower-purple.png" alt="">
          <ul>
            <li>Scrolling animations are not currently available in Firefox. This demo is using a polyfill.</li>
            <li>This layout is fragile due to the use of <code class="property">position: fixed</code>. You need to carefully manage stacking contexts.</li>
            <li>Snapping points have their own caveats, such as content taller than the viewport becoming inaccessible, along with the general annoyance of scrolljacking.</li>
            <li>The <code class="selector">blink</code> effect uses the <code class="property">contrast()</code> filter, which modifies the colors of the entire section. Thus, the background is set to black (or white), ensuring that it appears unchanged during transitions due to already being at maximum contrast.</li>
            </ul>
        </div>
      </div>
    </section>

  </main>

  <footer>
    <p>That's it <span class="emoji">🌸</span></p>
  </footer>
              
            
!

CSS

              
                @layer base, rhythm, layout, components, default, overwrites;

html {
  /* Create a snapping rule on the html element */
  scroll-snap-type: y mandatory;

  /* Create a timeline scope, so we can target any element on the page */
  timeline-scope: --section, --main, --site-header;
}

/* We'll want to match these colors for the best melty effect */
/* But play around with them (and add a color to section) to see happens */
body,
.content {
  background-color: var(--color-background, black);
  
}

main {
  view-timeline: --main;
}

.section {
  /* Creating a snapping rule on the section element */
  scroll-snap-align: start;
  scroll-snap-stop: always;

  /* Attach the timeline to the section element*/
  view-timeline: --section;

  /* Set each section to the full dynamic height of the viewport */
  height: 100dvh;
}

.content {
  /* Fix the content, so it doesn't scroll with the section */
  overflow: hidden;
  position: fixed;
  inset: 0;

  /* Animate the content based on the section scrolling */
  --contrast: 4;
  --blur: 0.5rem;

  animation: blink ease-in-out both;
  animation-timeline: --section;
}

@keyframes blink {
  0%,
  100% {
    filter: blur(var(--blur)) contrast(var(--contrast));
    opacity: 0;
    visibility: hidden;
  }

  50% {
    filter: blur(0) contrast(1);
    opacity: 1;
    visibility: visible;
  }
}

/* 
  This is a bit of a hack to get the indicator to work because I'm lazy.
  We're translating the dot from the top to the bottom of its parent,
  using the browser scroll position as the animation timeline.
  It's not really matched up to the scrolling sections, only appears to be.
*/
.indicator::before {
  animation: indicate linear both;
  animation-timeline: --main;
  animation-range: contain;
}

/* And we're manually setting the colors because see: lazy comment above */
@keyframes indicate {
  0% {
    --color-indicator: var(--color-primary);
    transform: translateY(0);
  }

  25% {
    --color-indicator: var(--color-yellow);
  }

  50% {
    --color-indicator: var(--color-secondary);
  }

  75% {
    --color-indicator: var(--color-red);
  }

  100% {
    --color-indicator: var(--color-purple);
    transform: translateY(
      calc(var(--indicator-total-height) - var(--indicator-size))
    );
  }
}

/* Remove gradient indicator on scroll-to-end (visible on small screens */

.site-header label:last-of-type {
  view-timeline: --site-header inline;
}

.site-header::after {
  animation: fade-scroll ease-in-out both;
  animation-timeline: --site-header;
  animation-range: entry-crossing;
}

@keyframes fade-scroll {
  0% {
    opacity: 1;
  }

  100% {
    opacity: 0;
  }
}

/* Change animation based on radio checked */
body:has([value="horizontal-scroll"]:checked) .content {
  /* 
    The only reason we're repeat these two properties instead of simply 
    setting `animation-name` is so the polyfill will pick them up 
    They're flaky though and tend to get stuck. You might need to
    refresh the page and select an option before scrolling
  */
  animation: horizontal-scroll ease-in-out both;
  animation-timeline: --section;
}

body:has([value="backwards-scroll"]:checked) .content {
  animation: backwards-scroll ease-in-out both;
  animation-timeline: --section;
}

body:has([value="zoom-scroll"]:checked) .content {
  animation: zoom-scroll ease-in-out both;
  animation-timeline: --section;
}

/* Alternative animations */
/* Very cool, try it */
@keyframes horizontal-scroll {
  0% {
    transform: translate3d(100%, 0%, 0);
  }

  50% {
    transform: none;
  }

  100% {
    transform: translate3d(-100%, 0%, 0);
  }
}

/* Befuddling, try it */
@keyframes backwards-scroll {
  0% {
    transform: translate3d(0%, -100%, 0);
  }

  50% {
    transform: none;
  }

  100% {
    transform: translate3d(0%, 100%, 0);
  }
}

/* WIP */
@keyframes zoom-scroll {
  0% {
    filter: blur(5rem);
    transform: scale(0);
    opacity: 0;
    visibility: hidden;
  }

  50% {
    filter: blur(0);
    transform: none;
    opacity: 1;
    visibility: visible;
  }

  100% {
    filter: blur(3rem);
    transform: scale(1.5);
    opacity: 0;
    visibility: hidden;
  }
}

/* 
  The actual page styling is in a different stylesheet to not clutter
  this pen with irrelevant or confusing code

  https://codepen.io/giana/pen/rNRzgRj
*/
              
            
!

JS

              
                
              
            
!
999px

Console