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

              
                <h1>Scroll Snap Event Visualizer</h1>
<main>
  <div>
    <p>Snap Change</p>
    <output id="snapchange" aria-live="polite"></output>
  </div>
  <section id="scroller" tabindex="0">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
    <div>8</div>
    <div>9</div>
    <div>10</div>
  </section>
  <div>
    <p>Snap Changing</p>
    <output id="snapchanging" aria-live="polite"></output>
  </div>
</main>

<footer>
  <label for="snapalign">Align to:</label>
  <select id="snapalign">
    <option>center</option>
    <option>start</option>
    <option>end</option>
  </select>
</footer>
              
            
!

CSS

              
                @import "https://unpkg.com/open-props" layer(support.design-system);
@import "https://unpkg.com/open-props/normalize.min.css" layer(support.demo);

::view-transition-group(.message) {
  animation-duration: .5s;
  animation-timing-function: var(--ease-3);
}

/* allow interrupting the view transition with mouse */
::view-transition {
  pointer-events: none;
}

@layer support.demo {
  html {
    view-transition-name: none;
  }
  
  body {
    display: grid;
    place-items: center;
    place-content: center;
    padding: var(--size-5);
    gap: var(--size-5);
  }
  
  main {
    display: grid;
    gap: var(--size-5);
    grid-template-columns: max-content 1fr max-content;
    grid-template-rows: min(250px, 30vh);
    
    @media (width <= 1000px) {
      grid-template-rows: 20ch 30vh 20ch;
      grid-template-columns: calc(90vh - 40ch);
    }
    
    > div {
      display: grid;
      justify-content: center;
      grid-template-rows: auto 1fr;
      
      > p {
        border: 1px solid var(--surface-3);
        padding: var(--size-2) var(--size-3);
      }
    }
  }
  
  section {
    display: grid;
    grid-auto-flow: column;
    inline-size: 100%;
    max-inline-size: min(1280px, 100vw);
    justify-self: center;
    
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scroll-snap-type: x mandatory;
    
    background: light-dark(white, var(--gray-10));
    padding: var(--size-3);
    scroll-padding-inline: var(--size-3);
    gap: var(--size-3);
    border: 1px solid var(--surface-3);
    
    &::before, &::after {
      content: "";
      display: block;
      inline-size: 50vw;
    }
    
    > * {
      inline-size: 200px;
      scroll-snap-align: var(--_snap-align, center);
      border: 1px solid var(--surface-2);
      
      display: grid;
      place-items: center;
      font-size: var(--font-size-6);
      color: var(--surface-4);
      padding: var(--size-2);
      
      &.changing {
        background: var(--surface-2);
        color: var(--text-1);
      }
      
      &.change {
        background: var(--indigo-5);
        color: white;
      }
    }
  }
  
  output {
    border: 1px solid var(--surface-2);
    border-block-start: none;
    display: grid;
    align-content: start;
    gap: var(--size-1);
    padding: var(--size-2);
    scroll-padding: var(--size-3);
    
    overflow-y: auto;
    overscroll-behavior-y: contain;
    scroll-snap-type: y mandatory;
    scroll-behavior: smooth;
    
    &:not(:focus-within, :hover) > :last-child {
      scroll-snap-align: end;
    }
    
    > * {
      transition: opacity .75s var(--ease-spring-2), transform .75s var(--ease-spring-2);
      background: var(--surface-3);
      border-radius: var(--radius-2);
      padding-inline: var(--size-2);
      padding-block: var(--size-1);
      font-size: var(--font-size-0);
      
      @starting-style {
        opacity: 0;
        transform: translateX(-100%);
      }
    }
  }
  
  #snapchange > :last-child {
    color: white;
    background: var(--indigo-5);
  }
  
  footer {
    display: flex;
    place-items: center;
    gap: 1ch;
    
    > select {
      background: var(--surface-2);
      padding-inline-end: var(--size-2);
    }
  }
  
  h1 {
    font-size: max(2.5vw, 2rem);
  }
}
              
            
!

JS

              
                const state = {
  changed: null,
  changing: null,
  queue: [],
  count: 0,
}

scroller.addEventListener('scrollsnapchange', event => {
  // create the log node
  const message = document.createElement('div')
  message.innerText = 'Changed to ' + event.snapTargetInline.innerText
  message.style.viewTransitionName = '--message-' + state.count++
  message.style.viewTransitionClass = 'message'
  snapchange.appendChild(message)
  
  // manage snap selected state
  state.changed?.classList.remove('change')
  event.snapTargetInline.classList.add('change')
  state.changed = event.snapTargetInline
  
  setTimeout(() => {
    state.queue.push({
      parent: snapchange,
      child: message,
    })
  }, 3000)
})

scroller.addEventListener('scrollsnapchanging', event => {
  // create the log node
  const message = document.createElement('div')
  message.innerText = 'Changing to ' + event.snapTargetInline.innerText
  message.style.viewTransitionName = '--message-' + state.count++
  message.style.viewTransitionClass = 'message'
  snapchanging.appendChild(message)
  
  // manage snapping state
  state.changed?.classList.remove('change')
  state.changing?.classList.remove('changing')
  event.snapTargetInline.classList.add('changing')
  state.changing = event.snapTargetInline
  
  setTimeout(() => {
    state.queue.push({
      parent: snapchanging,
      child: message,
    })
  }, 3000)
})

setInterval(() => {
  if (!state.queue.length) return
  
  document?.startViewTransition 
    ? document.startViewTransition(() => {
        let node = state.queue.shift()
        node.parent.removeChild(node.child)
      })
    : snapchanging.removeChild(state.queue.shift())
}, 1000)

snapalign.oninput = event => {
  document.body.style.setProperty('--_snap-align', event.target.selectedOptions[0].innerText)
}
              
            
!
999px

Console