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

              
                <div id="frame">
  <ul id="slides">
    <li id="0-0" style="background-color: red"></li>
    <li id="0-1" style="background-color: blue"></li>
    <li id="0-2" style="background-color: green"></li>
    <li id="1-0" style="background-color: gold"></li>
    <li id="1-1" style="background-color: lightgray"></li>
    <li id="1-2" style="background-color: yellow"></li>
    <li id="2-0" style="background-color: brown"></li>
    <li id="2-1" style="background-color: pink"></li>
    <li id="2-2" style="background-color: orange"></li>
  </ul>
</div>
              
            
!

CSS

              
                * { 
  margin: 0;
  padding: 0; 
  box-sizing: border-box; 
}

#frame {
  margin: auto;
  overflow: hidden;
  width: 100dvw;
  height: 100dvh;
  max-width: 70dvh;
}

#slides {
  display: grid;
  grid-template-columns: repeat(3, 100%);
  grid-template-rows: repeat(3, 100%);
  grid-column-gap: 0;
  grid-row-gap: 0;
  transition: all 500ms cubic-bezier(0.075, 0.820, 0.165, 1.000);
}

#slides li {
  display: inline-block;
  width: 100dvw;
  height: 100dvh;
  max-width: 70dvh;
}
              
            
!

JS

              
                const container = document.getElementById('slides');
const frame = document.getElementById('frame');

// touch and mouse are different events
const startDragEvents = ['mousedown', 'touchstart'];
const doDragEvents = ['mousemove', 'touchmove'];
const stopDragEvents = ['mouseup', 'mouseleave', 'touchstop', 'touchcancel', 'touchend'];

const width = 3; // slides per row
const height = 3; // slides per column

// cursor position during drag
const pos = { top: 0, left: 0, x: 0, y: 0 };
let curPaneX = 1, // start on center slide
    curPaneY = 1;
const dragThreshold = 80; // drag more pixels than that and we scroll to the next pane

// get first pointer, also in case of multi touch
const getPoint = e => e.changedTouches?.[0] || e;

const scrollToPane = (x, y) => {
  curPaneX = Math.min(Math.max(0, x), width-1);
  curPaneY = Math.min(Math.max(0, y), height-1);
  scrollIntoView(curPaneX, curPaneY);
}

let curTranslateX = 0, curTranslateY = 0;
const scrollIntoView = (x, y) => {
  const panelWidth = frame.offsetWidth;
  const panelHeight = frame.offsetHeight;
  curTranslateX = x*-panelWidth;
  curTranslateY = y*-panelHeight;
  container.style.transform = 
    `translate(${curTranslateX}px, ${curTranslateY}px)`;
}

const doDrag = e => {
  e.preventDefault();
  e = getPoint(e);
  // How far the mouse has been moved
  const dx = e.clientX - pos.x;
  const dy = e.clientY - pos.y;
  container.style.transform = 
    `translate(${curTranslateX + dx}px, ${curTranslateY + dy}px)`;
}

const scrollToBestPane = e => {
  const offsetX = e.clientX - pos.x;
  const offsetY = e.clientY - pos.y;
  if (curPaneX < (width - 1) && offsetX < -dragThreshold) curPaneX++;
  else if (curPaneX > 0 && offsetX > dragThreshold) curPaneX--;
  if (curPaneY < (height - 1) && offsetY < -dragThreshold) curPaneY++;
  else if (curPaneY > 0 && offsetY > dragThreshold) curPaneY--;
  scrollToPane(curPaneX, curPaneY);
}

    // start drag: save start point
  const startDrag = e => {
    e.preventDefault();
    e = getPoint(e);
    frame.style.cursor = 'grabbing';
    // The current scroll
    pos.left = container.scrollLeft;
    pos.top = container.scrollTop;
    // Get the current mouse position
    pos.x = e.clientX;
    pos.y = e.clientY;
    
    doDragEvents.forEach(n => frame.addEventListener(n, doDrag));
    stopDragEvents.forEach(n => frame.addEventListener(n, stopDrag));
  };


  
const stopDrag = e => {
  e.preventDefault();
  e = getPoint(e);
  frame.style.cursor = 'grab';
  doDragEvents.forEach(n => frame.removeEventListener(n, doDrag));
  stopDragEvents.forEach(n => frame.removeEventListener(n, stopDrag));
  scrollToBestPane(e);
}

startDragEvents.forEach(n => frame.addEventListener(n, startDrag));
scrollToPane(curPaneX, curPaneY); // init in the center

              
            
!
999px

Console