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="root"></div>
              
            
!

CSS

              
                #root {
  padding: 20px;
}

.vertical-gallery {
  &.gallery-container {
    display: flex;
    position: relative;
    justify-content: center;
    margin: 0 60px 0 80px;
    width: 100%;
    max-width: calc((100vh - 230px) * 0.75);
    height: fit-content;

    .thumbnails {
      display: flex;
      position: absolute;
      top: 0;
      left: -80px;
      flex-direction: column;
      transform: none;
      gap: 4px;
    }
  }

  &.single-image {
    .thumbnails,
    .scrollbar {
      display: none;
    }

    .slides {
      overflow-y: hidden;
    }
  }

  .thumbnails div {
    position: relative;
    width: 48px;
    height: 64px;
    border-radius: 2px;
    cursor: pointer;

    img {
      width: 100%;
      height: auto;
      border-radius: 2px;
      filter: brightness(0.97);
    }

    &.selected {
      opacity: 0.5;
    }

    &.more-images {
      height: auto;
      cursor: pointer;
      padding: 10px 0;
      text-align: center;
      color: #363636;
    }
  }

  .scrollbar {
    display: block;
    position: absolute;
    top: 0;
    left: -16px;
    width: 1px;
    height: 100%;
    background: #ccc;
  }

  .thumb {
    position: absolute;
    transition: 0.3s height ease-in-out;
    width: 1px;
    height: 0;
    background: #000;
  }

  .slide-container {
    width: 100%;
    height: 0;
    padding-top: 133.333%;
  }

  .slides {
    scrollbar-width: none;

    &::-webkit-scrollbar {
      display: none;
    }

    display: grid;
    position: absolute;
    top: 0;
    left: 0;
    grid-auto-flow: row;
    height: 100%;
    overflow-y: hidden;
    gap: 0;
    scroll-snap-type: y mandatory;
    scroll-behavior: smooth;

    // Clever hack to avoid MacOS inertia breaking the scroll
    &:hover {
      overflow-y: auto;
    }

    &:not(:hover) {
      animation: 0.3s delay-overflow cubic-bezier(0.35, -0.7, 1, 1);
    }

    > div {
      display: flex;
      position: relative;
      scroll-snap-align: start;

      &::after {
        content: "";
        position: absolute;
        top: 0;
        left: 0;
        opacity: 0.03;
        width: 100%;
        height: 100%;
        background-color: #000;
        pointer-events: none;
      }
    }

    img {
      width: 100%;
      cursor: pointer;
      object-fit: contain;

      &:first-of-type {
        min-height: 100%;
      }
    }
  }
}

@keyframes delay-overflow {
  from {
    overflow: auto;
  }
}

              
            
!

JS

              
                class VerticalGallery extends React.PureComponent {
  state = {
    selectedIndex: 0,
    galleryHeight: 120,
    isScrolling: false
  };

  galleryRef = React.createRef();

  thumbnailsRef = React.createRef();

  intervalRef = null;

  componentDidMount() {
    this.updateGalleryHeight();
    this.intervalRef = setInterval(() => this.snapGallery(), 300);
  }

  componentDidUpdate() {
    this.updateGalleryHeight();
  }

  componentWillUnmount() {
    clearInterval(this.intervalRef);
  }

  updateGalleryHeight() {
    const gallery = this.galleryRef.current;
    if (gallery) {
      this.setState({ galleryHeight: gallery.offsetHeight });
    }
  }

  snapGallery() {
    const gallery = this.galleryRef.current;
    const thumbnails = this.thumbnailsRef.current;
    const { selectedIndex, galleryHeight, isScrolling } = this.state;
    if (
      !gallery.matches(":hover") &&
      !thumbnails.matches(":hover") &&
      !isScrolling
    ) {
      this.setState({ isScrolling: true }, () => {
        setTimeout(() => this.setState({ isScrolling: false }), 300);
      });
      gallery.scrollTop = galleryHeight * selectedIndex;
    }
  }

  render() {
    const { images, displayTitle } = this.props;
    const { selectedIndex, galleryHeight, isScrolling } = this.state;
    if (!images) return null;
    const imageCount = images.length;
    const imageScrollHeight = galleryHeight / imageCount;
    const maxThumbnails = Math.floor(galleryHeight / 68);
    let visibleThumbnails = maxThumbnails;
    if (maxThumbnails < imageCount) {
      if (galleryHeight - 40 < maxThumbnails * 68) visibleThumbnails -= 1;
    }
    const isFirstRender = !this.galleryRef.current;

    return (
      <div className="vertical-gallery gallery-container">
        <div className="thumbnails" ref={this.thumbnailsRef}>
          {images.slice(0, visibleThumbnails).map((image, index) => {
            const selected = index === selectedIndex;
            return (
              <div
                key={`thumbnail_${image.imageUrl}`}
                className={selected ? "selected" : undefined}
                onClick={() => {
                  this.setState(
                    { selectedIndex: index, isScrolling: true },
                    () => {
                      setTimeout(
                        () => this.setState({ isScrolling: false }),
                        Math.abs(selectedIndex - index) * 150 + 300
                      );
                    }
                  );
                  this.galleryRef.current.scrollTop = galleryHeight * index;
                }}
              >
                <img alt="" src={image.imageUrl} />
              </div>
            );
          })}
          {!isFirstRender && imageCount > visibleThumbnails && (
            <div
              className="more-images"
              onClick={() => {
                this.openModal(0);
              }}
            >
              <span>+{imageCount - visibleThumbnails}</span>
            </div>
          )}
        </div>
        <div className="scrollbar">
          <div
            className="thumb"
            style={{ height: `${(selectedIndex + 1) * imageScrollHeight}px` }}
          />
        </div>
        <div className="slide-container">
          <div
            className="slides"
            ref={this.galleryRef}
            onScroll={() => {
              if (!isScrolling) {
                const { scrollTop } = this.galleryRef.current;
                const index = Math.round(scrollTop / galleryHeight);
                this.setState({ selectedIndex: index });
              }
            }}
          >
            {images
              .slice(0, isFirstRender ? 1 : undefined)
              .map((image, index) => (
                <div
                  key={image.imageUrl}
                  className="slide"
                  onClick={() => {
                    this.openModal(index);
                  }}
                >
                  <img src={image.imageUrl} alt={displayTitle} />
                </div>
              ))}
          </div>
        </div>
      </div>
    );
  }
}

const props = {
  images: [
    { imageUrl: "https://picsum.photos/id/1067/540/720" },
    { imageUrl: "https://picsum.photos/id/122/540/720" },
    { imageUrl: "https://picsum.photos/id/188/540/720" },
    { imageUrl: "https://picsum.photos/id/249/540/720" },
    { imageUrl: "https://picsum.photos/id/257/540/720" },
    { imageUrl: "https://picsum.photos/id/259/540/720" },
    { imageUrl: "https://picsum.photos/id/283/540/720" },
    { imageUrl: "https://picsum.photos/id/288/540/720" },
    { imageUrl: "https://picsum.photos/id/299/540/720" }
  ],
  displayTitle: "Lorem ipsum"
};

ReactDOM.render(
  <VerticalGallery {...props} />,
  document.getElementById("root")
);

              
            
!
999px

Console