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

Save Automatically?

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

              
                <section class="cb-image-grid cb-image-grid--type-one">
  <div class="cb-image-grid__container padding-top-md padding-bottom-md">
    <div class="cb-image-grid__images-container">
      <div class="cb-image-grid__image" lightbox-toggle>
        <img src="http://via.placeholder.com/350x350/DB4E88/fff?text=image+one" loading="lazy">
      </div>
      <div class="cb-image-grid__image" lightbox-toggle>
        <img src="http://via.placeholder.com/350x350/7176A7/fff?text=image+two" loading="lazy">
      </div>
      <div class="cb-image-grid__image" lightbox-toggle>
        <img src="http://via.placeholder.com/350x350/6448DA/fff?text=image+three" loading="lazy">
      </div>
      <div class="cb-image-grid__image" lightbox-toggle>
        <img src="http://via.placeholder.com/350x350/53CE00/fff?text=image+four" loading="lazy">
      </div>
      <div class="cb-image-grid__image" lightbox-toggle>
        <img src="http://via.placeholder.com/350x350/CBC207/fff?text=image+five" loading="lazy">
      </div>
      <div class="cb-image-grid__image" lightbox-toggle>
        <img src="http://via.placeholder.com/350x350/2EB1F9/fff?text=image+six" loading="lazy">
      </div>
      <div class="cb-image-grid__image" lightbox-toggle>
        <img src="http://via.placeholder.com/350x350/13A5D6/fff?text=image+seven" loading="lazy">
      </div>
    </div>
  </div>
</section>
              
            
!

CSS

              
                .cb-image-grid {
  $cb: &;

  background-color: #333;
  color: white;
  font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;

  &--type-one {
    #{$cb}__container {
      margin: 0 auto;
      text-align: center;
      padding-left: 40px;
      padding-right: 40px;
      padding-bottom: 60px;
      padding-top: 60px;

      #{$cb}__images-container {
        column-gap: 1rem;
        columns: 2 200px;
      }

      #{$cb}__image {
        position: relative;
        overflow: hidden;
        width: 150px;
        margin: 0 1rem 1rem 0;
        display: inline-block;
        width: 100%;
        max-height: 90vh;

        &:hover {
          img {
            transform: scale(1.075);
          }
        }

        img {
          border-radius: 4px;
          overflow: hidden;
          z-index: 0;
          position: absolute;
          top: 0;
          left: 0;
          height: 100%;
          width: 100%;
          object-fit: cover;
          transition: transform 0.6s;
          will-change: transform;
        }

        @for $i from 1 through 100 {
          &:nth-child(#{$i}) {
            $h: (random(400) + 200) + px;
            height: $h;
          }
        }
      }
    }
  }
}

[lightbox-toggle] {
  cursor: zoom-in;

  &:after {
    position: absolute;
    content: url('data:image/svg+xml; utf8, <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path fill="white" stroke="black" stroke-opacity="0.25" stroke-width="0.25" d="M16.32 14.9l5.39 5.4a1 1 0 0 1-1.42 1.4l-5.38-5.38a8 8 0 1 1 1.41-1.41zM10 16a6 6 0 1 0 0-12 6 6 0 0 0 0 12zm1-7h2a1 1 0 0 1 0 2h-2v2a1 1 0 0 1-2 0v-2H7a1 1 0 0 1 0-2h2V7a1 1 0 1 1 2 0v2z"/></svg>');
    height: 32px;
    width: 32px;
    bottom: 0;
    right: 0;
    opacity: 0;
    will-change: opacity;
    transition: opacity 0.2s;
  }

  &:hover {
    &:after {
      opacity: 1;
      filter: drop-shadow(2px 4px 6px black);
    }
  }
}
.c-lightbox {
  $c: &;

  font-family: Arial, "Helvetica Neue", Helvetica, sans-serif;
  pointer-events: none;
  position: fixed;
  opacity: 0;
  width: 100vw;
  height: 100vh;
  z-index: 9000000;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.85);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  transition: opacity 0.3s;

  &.open {
    opacity: 1;
    pointer-events: all;
  }

  #{$c}__container {
    width: 100%;
    height: 100%;
  }

  #{$c}__close {
    z-index: 999999;
    position: absolute;
    cursor: pointer;
    top: 1vh;
    right: 1vw;
    font-size: 30px;
    padding: 20px;
    color: white;

    &:hover {
      color: red;
    }
  }

  .swiper-container {
    width: 100%;
    height: 100%;
  }

  .swiper-slide {
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .swiper-pagination {
    color: white;
  }

  .swiper-button-next,
  .swiper-button-prev {
    color: white;

    &:hover {
      color: red;
    }

    &:after {
      font-size: 30px;
    }
  }

  #{$c}__image {
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    height: 75%;
    width: 75%;
    text-align: center;
    cursor: zoom-in;

    img {
      width: auto;
      height: auto;
      max-height: 90vh;
      max-width: 90vw;
    }
  }
}

              
            
!

JS

              
                let swiper;
window.globals = {};
const body = document.getElementsByTagName("body")[0];
const lightboxImages = document.querySelectorAll("[lightbox-toggle] img");
const initLightbox = lightboxImages.length > 0;
const destroySwiper = (swiper, timeout) => {
  setTimeout(() => {
    swiper.destroy();
  }, timeout);
};

const createLightboxSkeleton = () => {
  // Create skeleton for lightbox
  const lightbox = document.createElement("div");
  const lightboxContainer = document.createElement("div");
  const lightboxClose = document.createElement("div");
  const swiperContainer = document.createElement("div");
  const swiperWrapper = document.createElement("div");
  const swiperBtnNext = document.createElement("div");
  const swiperBtnPrev = document.createElement("div");
  const swiperPagination = document.createElement("div");

  // Add classes
  lightbox.classList.add("c-lightbox");
  lightboxContainer.classList.add("c-lightbox__container");
  lightboxClose.classList.add("c-lightbox__close");
  lightboxClose.setAttribute("tabindex", "0");
  lightboxClose.innerHTML = "X";
  swiperContainer.classList.add("swiper-container");
  swiperWrapper.classList.add("swiper-wrapper");
  swiperBtnNext.classList.add("swiper-button-next");
  swiperBtnPrev.classList.add("swiper-button-prev");
  swiperPagination.classList.add("swiper-pagination");

  // Append created divs
  lightboxContainer.appendChild(lightboxClose);
  swiperContainer.appendChild(swiperWrapper);
  swiperContainer.appendChild(swiperBtnNext);
  swiperContainer.appendChild(swiperBtnPrev);
  swiperContainer.appendChild(swiperPagination);
  lightboxContainer.appendChild(swiperContainer);
  lightbox.appendChild(lightboxContainer);
  body.appendChild(lightbox);

  // Set variables to reference the lightbox
  globals.lightboxRef = document.querySelector(".c-lightbox");
  globals.swiperWrapperRef = document.querySelector(
    ".c-lightbox .swiper-wrapper"
  );
};

if (initLightbox) {
  createLightboxSkeleton();

  // The rest of the code will go here
  lightboxImages.forEach(function (el, index) {
    // Add click function to lightbox images
    el.addEventListener("click", imageClick, false);

    function imageClick() {
      // Clear swiper before trying to add to it
      globals.swiperWrapperRef.innerHTML = "";

      // Loop through images with lightbox data attr
      // Create html for lightbox carousel
      lightboxImages.forEach(function (img) {
        console.log(index);
        // Create clone of current image in loop
        const image = img.cloneNode(true);
        // Create divs
        const slide = document.createElement("div");
        const imageContainer = document.createElement("div");
        // Add classes
        slide.classList.add("swiper-slide");
        imageContainer.classList.add("c-lightbox__image");
        // Append images to the slides, then slides to swiper wrapper
        imageContainer.appendChild(image);
        slide.appendChild(imageContainer);
        globals.swiperWrapperRef.appendChild(slide);
      });

      // Init Swiper
      swiper = new Swiper(".c-lightbox .swiper-container", {
        initialSlide: index,
        loop: true,
        slidesPerView: 1,
        speed: 750,
        spaceBetween: 16,
        watchOverflow: true,
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev"
        },
        pagination: {
          el: ".swiper-pagination",
          type: "fraction"
        },
        zoom: true,
        fadeEffect: {
          crossFade: false
        },
        keyboard: {
          enabled: true,
          onlyInViewport: true
        },
        mousewheel: {
          sensitivity: 1,
          forceToAxis: true,
          invert: true
        }
      });

      // Add the class to open the lightbox
      // Add overflow hidden to the body to prevent scrolling
      globals.lightboxRef.classList.add("open");
      body.classList.add("overflowHidden");
    }
  });

  // Close lightbox on click
  document.addEventListener(
    "click",
    ({ target }) => {
      if (target.matches(".c-lightbox__close")) {
        destroySwiper(swiper, 250);
        globals.lightboxRef.classList.remove("open");
        body.classList.remove("overflowHidden");
      }
    },
    false
  );

  // Close lightbox on escape key press
  document.addEventListener("keydown", ({ key }) => {
    if (key === "Escape") {
      destroySwiper(swiper, 250);
      globals.lightboxRef.classList.remove("open");
      body.classList.remove("overflowHidden");
    }
  });
}

              
            
!
999px

Console