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

              
                <main class="the-frames">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div class="primary"></div>
</main>

<form>
  <div>[Drag left &amp; right]</div>
  <div>
    <input id="show" type="checkbox" checked>
    <label for="show">Show Cover</label>
  </div>
  <div><a href="https://codepen.io/danwilson/details/oWmQzb/" target="_blank">Breakdown Steps</a></div>
</form>
              
            
!

CSS

              
                :root {
  --pixel: 1px;
  --cover: #212123;
  --covered: calc(var(--pixel) * 5); /*6 frames, 5 covered at a time */
  --space: white; /* will cause transparency when mix blend mode multiply */
  --w: 100vw;
  --h: 100vh;
  
  --blend: multiply;
}
.highres {
  --pixel: .5px;
}
main div {
  --offset: calc(var(--pixel) * 1);
  
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: hidden;
  background-color: #16161c;
  background-image: 
    linear-gradient(to right, 
      transparent 0,
      transparent calc(var(--pixel)),
      var(--space) var(--pixel),
      var(--space) calc(var(--covered))),
    var(--background-image);
  
  background-position: 
    calc(var(--offset) - calc(var(--pixel) * 1)) 0,
    center center;
  background-size: 
    calc(var(--pixel) * 6) 100%,
    cover;
  background-repeat: 
    repeat,
    no-repeat;
  
  mix-blend-mode: var(--blend);
  
  /* counter the black lines by brightening the images */
  filter: saturate(200%);
}

/*want to try using your own frames? insert them here, with either a gradient or a url() */
main div:nth-of-type(1) {
  --background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/61811/h1.jpg);
}
main div:nth-of-type(2) {
  --offset: calc(var(--pixel) * 2);
  --background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/61811/h2.jpg);
}
main div:nth-of-type(3) {
  --offset: calc(var(--pixel) * 3);
  --background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/61811/h3.jpg);
}
main div:nth-of-type(4) {
  --offset: calc(var(--pixel) * 4);
  --background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/61811/h4.jpg);
}
main div:nth-of-type(5) {
  --offset: calc(var(--pixel) * 5);
  --background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/61811/h5.jpg);
}
main div:nth-of-type(6) {
  --offset: calc(var(--pixel) * 6);
  --background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/61811/h6.jpg);
}



/*the sliding cover that triggers the motion illusion */
main::after {
  content: '';
  position: absolute;
  top: 0;
  right: calc(var(--pixel) * -12);
  bottom: 0;
  left: calc(var(--pixel) * -12);
  
  background: linear-gradient(to right,
    transparent 0px,
    transparent calc(var(--pixel) * 1),
    var(--cover) calc(var(--pixel) * 1),
    var(--cover) calc(var(--pixel) * 6));
  background-size: calc(var(--pixel) * 6) 100%;
  
  will-change: transform, opacity;
  transition: opacity 800ms linear;
  opacity: var(--show, 1);
  
  mix-blend-mode: normal;
  
  /* update this via JS on drag or orientationchange */
  transform: translateX(var(--slide-amount, 0));
}




main {
  width: var(--w);
  height: var(--h);
  position: relative;
  background: white;
}
*, *::after {
  box-sizing: border-box;
}










body {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: hsl(203, 50%, 4%);
  margin: 0;
  overflow: hidden;
  touch-action: none;
  
  font-family: system-ui, 'Segoe UI', sans-serif
}
form {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  padding: .2rem 1rem;
  text-align: center;
  
  background: rgba(245,245,255,.86);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  --form-color-alpha: rgba(150, 250, 255, .1);
}
form div {
  padding: .5rem .7rem;
  font-size: 12px;
}
form label {
  transform: translateX(-1.5rem)
}



              
            
!

JS

              
                //checkAndSetPixelSize();

var width = window.innerWidth;
var style = document.documentElement.style;
var pixelAmount = parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--pixel').trim());

if (window.PointerEvent) {
  document.body.addEventListener('pointermove', moveIt);
} else {
  document.body.addEventListener('touchmove', moveIt);
  document.body.addEventListener('mousemove', moveIt);
}


function moveIt(e) {
  e.preventDefault();
  var amount = Math.min(Math.floor((e.clientX || e.touches[0].clientX) / width * 6), 5);
  style.setProperty('--slide-amount', (amount * pixelAmount) + 'px');
}

window.addEventListener('resize', function() {
  width = window.innerWidth;
});

function orientIt(e) {
  var amount = (e.gamma / 12);
  style.setProperty('--slide-amount', (amount * pixelAmount) + 'px')
}

//Support for accelerometer around Y axis (e.g. in your hand facing you tilting it to the left and the right... best when orientationlocked). Disable when pointer events are happening.
if (window.DeviceOrientationEvent) {
  window.addEventListener('deviceorientation', orientIt);
  
  if (window.PointerEvent) {
    document.body.addEventListener('pointerdown', stopOrientation);
    
    document.body.addEventListener('pointerup', startOrientation);
    document.body.addEventListener('pointercancel', startOrientation);
    document.body.addEventListener('pointerleave', startOrientation);
  } else {
    document.body.addEventListener('touchstart', stopOrientation);
    
    document.body.addEventListener('touchend', startOrientation);
    document.body.addEventListener('touchcancel', startOrientation);
    
    document.body.addEventListener('mousedown', stopOrientation);
    
    document.body.addEventListener('mouseup', startOrientation);
    document.body.addEventListener('mouseleave', startOrientation);
  }
}

function stopOrientation(e) {
  window.removeEventListener('deviceorientation', orientIt);
}
function startOrientation(e) {
  window.addEventListener('deviceorientation', orientIt);
}

document.getElementById('show').addEventListener('click', function(e) {
  document.documentElement.style.setProperty('--show', e.currentTarget.checked ? 1 : 0);
});

function checkAndSetPixelSize() {
  if (window.devicePixelRatio && window.devicePixelRatio >= 2) {
    document.body.style.border = '.5px solid transparent';
    var size = getComputedStyle(document.body).borderWidth;
    if (size.indexOf('.5') > -1) {  
      console.log('looks to support .5px for high res screens');
      document.documentElement.classList.add('highres');
    }
  }
}
              
            
!
999px

Console