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

              
                .boxes
  - const COUNT = 20
  - let b = 0
  while b < COUNT
    .box= b + 1
    - b++
  label Index: 0
  button(data-control="next") Next
  button(data-control="previous") Previous
              
            
!

CSS

              
                *
  box-sizing border-box
  
body
  display grid
  place-items center
  min-height 100vh
  
.boxes
  height 100vh
  width 100vw
  
.box
  position absolute
  top 50%
  left 50%
  height 25vmin
  width 25vmin
  display grid
  place-items center
  font-size 5vmin
  font-family sans-serif
  transform translate(-50%, -50%)
  
  &:nth-of-type(odd)
    background hsl(10, 80%, 50%)
  
  &:nth-of-type(even)
    background hsl(210, 80%, 50%)
    
label
  font-size 2rem
  font-weight bold
  font-family sans-serif
  position absolute
  top calc(50% + 25vmin)
  left 50%
  transform translate(-50%, 0)
              
            
!

JS

              
                import gsap from "https://cdn.skypack.dev/gsap";
import ScrollTrigger from 'https://cdn.skypack.dev/gsap/ScrollTrigger'

gsap.registerPlugin(ScrollTrigger)

const stagger = 0.25;
const duration = 1;
const offset = 1.25;

let iteration = 0
let trigger

const BOXES = gsap.utils.toArray(".box");
const SHIFT_DURATION = (BOXES.length - 1) * stagger + duration
const SNAP = gsap.utils.snap(1 / BOXES.length)

const getShift = (zIndex) => {
  const SHIFT = gsap.timeline();
  for (let b = 0; b < BOXES.length; b++) {
    const TIME = b * stagger;
    SHIFT.set(BOXES[b], { zIndex, xPercent: -200, scale: 0 }, TIME)
    SHIFT.fromTo(
      BOXES[b],
      {
        xPercent: -200
      },
      {
        xPercent: 100,
        duration,
        ease: "none",
        immediateRender: false,
      },
      TIME
    ).fromTo(
      BOXES[b],
      {
        scale: 0
      },
      {
        scale: 1,
        repeat: 1,
        yoyo: true,
        ease: 'none',
        duration: duration * 0.5,
        immediateRender: false,
      },
      TIME
    );
  }
  return SHIFT;
};

const LOOP = gsap.timeline({
  paused: true
})
  .add(getShift(0), 0)
  .add(getShift(1), BOXES.length * stagger)
  .add(getShift(2), BOXES.length * stagger * 2)

const LOOP_HEAD = gsap.fromTo(LOOP, {
  totalTime: (SHIFT_DURATION - (duration - stagger)) + offset,
},
{
  totalTime: (SHIFT_DURATION * 2 - ((duration - stagger) * 2)) + offset,
  duration: 1,
  ease: 'none',
  repeat: -1,
  paused: true
})

const SCRUB = gsap.to(LOOP_HEAD, {
  totalTime: 0,
  paused: true,
  duration: 0.5,
  ease: 'power3'
})

const wrapForward = trigger => {
  console.info('WRAP')
  iteration++
  trigger.wrapping = true
  trigger.scroll(trigger.start + 1)
}
const wrapBackward = trigger => {
  iteration--
  if (iteration < 0) {
    iteration = 9
    LOOP_HEAD.totalTime(LOOP_HEAD.totalTime() + LOOP_HEAD.duration() * 10)
    SCRUB.pause()
  }
  trigger.wrapping = true
  trigger.scroll(trigger.end - 1)
}

const scrubTo = totalTime => {
  const PROGRESS = (totalTime - (LOOP_HEAD.duration() * iteration)) / LOOP_HEAD.duration()
  console.info(PROGRESS)
  if (PROGRESS > 1) {
    wrapForward(trigger)
  } else if (PROGRESS < 0) {
    wrapBackward(trigger)
  } else {
    trigger.scroll(trigger.start + PROGRESS * (trigger.end - trigger.start))
  }
}


trigger = ScrollTrigger.create({
  start: 0,
  end: '+=2000',
  horizontal: false,
  pin: '.boxes',
  onUpdate: self => {
    if (self.progress === 1 && self.direction > 0 && !self.wrapping) {
      wrapForward(self)
    } else if (self.progress < 1e-5 && self.direction < 0) {
      wrapBackward(self)
    } else {
      SCRUB.vars.totalTime = LOOP_HEAD.duration() * (iteration + self.progress)
      SCRUB.invalidate().restart()
      self.wrapping = false
    }
  }
})

const LABEL = document.querySelector('label')
ScrollTrigger.addEventListener("scrollEnd", () => {
  const SNAP_POINT = SNAP((iteration + trigger.progress) * LOOP_HEAD.duration())
  LABEL.innerText = `Index: ${SNAP_POINT}`
  scrubTo(SNAP_POINT)
});

const NEXT = document.querySelector('[data-control="next"]')
const PREV = document.querySelector('[data-control="previous"]')

NEXT.addEventListener('click', () => {
  const DESTINATION = SNAP(SCRUB.vars.totalTime + (1 / BOXES.length))
  scrubTo(DESTINATION)
})
PREV.addEventListener('click', () => {
  const DESTINATION = SNAP(SCRUB.vars.totalTime - (1 / BOXES.length))
  scrubTo(DESTINATION)
})
              
            
!
999px

Console