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

              
                <ul class="nav">
  <li class="active" id="all">All Images</li>
  <li  id="newYork" class="js-newyourk">New York City</li>
  <li id="flowers" class="js-">Flowers</li>
  <li  id="others">Others</li>
</ul>

<div class="gallery">
  <figure class="gallery--item flowers" style="--index: 1;">
    <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/sunFlowerNJ.jpg" alt="flowers">
  </figure>
  <figure class="gallery--item flowers" style="--index: 2;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/flowers.jpg" alt="flowers"></figure>
  <figure class="gallery--item newYork" style="--index: 3;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/nycGalaxy.jpg" alt="newYork"></figure>
  <figure class="gallery--item flowers" style="--index: 4;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/butterflyOnFlower.jpg" alt="flowers"></figure>
  <figure class="gallery--item newYork" style="--index: 5;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/nycBuilding2.jpg" alt="newYork"></figure>
  <figure class="gallery--item newYork" style="--index: 6;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/falafelPost.jpg" alt="newYork"></figure>
  <figure class="gallery--item newYork" style="--index: 7;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/littleRedLightHouse.jpg" alt="newYork"></figure>
  <figure class="gallery--item flowers" style="--index: 8;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/gwFlowerB%26W.jpg" alt="flowers"></figure>
  <figure class="gallery--item others" style="--index: 9;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/highwayExposure2.jpg" alt="others"></figure>
  <figure class="gallery--item flowers" style="--index: 10;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/flowerWithButterfly.jpg" alt="flowers"></figure>
  <figure class="gallery--item flowers" style="--index: 11;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/flowerWithBee.jpg" alt="flowers"></figure>
  <figure class="gallery--item newYork" style="--index: 12;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/oilTankerHudson.jpg" alt="newYork"></figure>
  <figure class="gallery--item flowers" style="--index: 13;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/flowersCloseUp2.jpg" alt="flowers"></figure>
  <figure class="gallery--item others" style="--index: 14;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/redMillB%26W.jpg" alt="others"></figure>
  <figure class="gallery--item newYork" style="--index: 15;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/commuteToWork.jpg" alt="newYork"></figure>
  <figure class="gallery--item others" style="--index: 16;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/miamiNeonSign.jpg" alt="others"></figure>
  <figure class="gallery--item flowers" style="--index: 17;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/redFlowerBleachBypass.jpg" alt="flowers"></figure>
  <figure class="gallery--item newYork" style="--index: 18;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/wrGraceBuilding.jpg" alt="newYork"></figure>
  <figure class="gallery--item newYork" style="--index: 19;"><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1362379/AmericanStandardBuilding.jpg" alt="newYork"></figure>
</div>
<div class="tutorials">
  <a href="https://s.juejin.cn/ds/idkRes2Y/" target="_blank">
    <svg t="1696988571607" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4422" width="200" height="200"><path d="M924.8 576.64v-115.2c12.8-13.44 13.44-26.88 12.8-34.56-3.2-30.08-36.48-42.24-47.36-46.72-79.36-30.08-226.56-89.6-307.84-122.88-49.28-24.32-65.28-19.84-113.28-0.64-12.8 5.12-224.64 83.84-306.56 117.76-44.8 18.56-51.2 46.08-51.2 61.44 0 25.6 17.92 48.64 50.56 62.08L259.84 537.6v257.28l8.32 8.32c3.84 3.84 91.52 92.16 252.16 92.16 150.4 0 236.16-88.96 240-92.8l7.68-8.32V540.16l36.48-15.36-1.28 52.48-29.44 49.28 88.96 84.48 98.56-85.76-36.48-48.64zM710.4 771.2c-22.4 18.56-88.96 67.2-190.08 67.2-111.36 0-179.84-48.64-202.88-67.2V561.28l144.64 58.88h0.64c46.08 17.28 81.28 16 125.44-3.2l122.24-51.84v206.08z m154.24-138.24l-17.92-16.64 13.44-22.4 2.56-102.4c4.48-14.08-0.64-25.6-8.32-32-7.68-7.04-21.76-11.52-36.48-2.56L565.12 563.84c-30.08 13.44-49.92 14.08-81.92 2.56l-298.88-121.6c-7.68-3.2-12.16-6.4-14.08-8.96 1.92-1.92 6.4-5.12 14.72-8.96 81.92-33.92 302.72-116.48 305.28-117.12h0.64c38.4-15.36 38.4-15.36 67.2-0.64l1.92 0.64c80.64 32.64 226.56 92.16 307.2 122.88v163.2l16 21.12-18.56 16z" p-id="4423"></path></svg>
  </a>
  
</div>
              
            
!

CSS

              
                @layer reset, base, layout, transitions;

@layer transitions {
  @keyframes scale-out {
    to {
      scale: 0;
    }
  }

  @keyframes fade-in {
    to {
      opacity: 1;
    }
  }

  :root {
    view-transition-name: none;
  }

  ::view-transition {
    pointer-events: none;
  }
}

@layer layout {
  body {
    gird-template-rows: min-content minmax(0, 1fr);
  }

  .nav {
    display: flex;
    padding: 1rem;
    background: #333;
    gap: 1rem;
    align-items: center;
    justify-content: center;
    border-bottom: 1px dashed #665e5e;
    box-shadow: 0 1px 0 0 #0c0a0a;

    & li {
      padding: 0.2em 0.5em;
      border: 2px dashed transparent;
      border-radius: 50%;
      border-radius: 4px;
      cursor: pointer;
      min-height: 44px;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      text-transform: uppercase;
      transition: all 0.2s linear;
      &:hover,
      &.active {
        border-color: #09f;
        color: #09f;
      }
    }
  }

  .gallery {
    padding: 1rem;
    display: grid;
    grid-template-columns: repeat(
      auto-fit,
      minmax(min(100% - 2rem, 280px), 1fr)
    );
    gap: 1rem;
    grid-auto-flow: dense;

    & figure {
      aspect-ratio: 4 / 3;
      border-radius: 10px;
      background: #18141b;
      box-shadow: rgb(0 0 0 / 0.24) 0px 3px 8px;
      position: relative;
      &.active {
        grid-area: span 2 / span 2;
      }
    }

    & img {
      position: absolute;
      inset: 0;
      border-radius: 10px;
      width: 100%;
      height: 100%;
    }
  }
}

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

  ul {
    list-style: none outside none;
  }

  img {
    display: block;
    max-width: 100%;
    height: auto;
    object-fit: cover;
    object-position: center;
  }
}

@layer base {
  body {
    width: 100vw;
    min-height: 100vh;
    font-family: "Exo", "Bungee Shade", cursive, Arial, sans-serif;
    background-color: #110d14;
    color: #fff;
    display: grid;
    align-content: start;
  }
  @media (prefers-reduced-motion: reduce) {
    *,
    ::before,
    ::after {
      animation-delay: -1ms !important;
      animation-duration: 1ms !important;
      animation-iteration-count: 1 !important;
      background-attachment: initial !important;
      scroll-behavior: auto !important;
      transition-duration: 0s !important;
      transition-delay: 0s !important;
    }
  }
  
  .tutorials {
    position: fixed;
    bottom: 1rem;
    right: 1rem;
    z-index: 9999;
    width: 64px;
    aspect-ratio: 1;
    border-radius: 50%;
    background: #3f517e;
    padding: .5rem;
    display: grid;
    place-content: center;
    transition: all .28s linear;
    
    &:hover {
      background:#4161b2;
    }
    
    & svg {
      display: block;
      width: 100%;
      height: 100%;
      filter: invert(1);
    }
  }
}

              
            
!

JS

              
                const gallery = document.querySelector(".gallery");
const galleryItems = document.querySelectorAll(".gallery--item");
const tablis = document.querySelectorAll(".nav > li");
const imgNy = document.querySelectorAll(".newYork");
const imgFlowers = document.querySelectorAll(".flowers");
const imgOthers = document.querySelectorAll(".others");

galleryItems.forEach((item) => {
  const transitionStyle = document.createElement("style");
  const index = getComputedStyle(item).getPropertyValue("--index");
  const groupName = `item-${index}`;
  item.style.viewTransitionName = groupName;
  item.appendChild(transitionStyle);
  transitionStyle.innerHTML = `
        @media (prefers-reduced-motion: no-preference) {
            ::view-transition-old(${groupName}) {
                animation: scale-out 200ms ease-out forwards;
            }
        
            ::view-transition-new(${groupName}) {
                animation: 
                  fade-in 100ms ease-in normal forwards, 
                  scale-out 400ms ease-in reverse none;
            }
        }

        ::view-transition-group(${groupName}) {
            animation-delay: ${index * 20}ms;
        }
    `;
  item.addEventListener('click', () => {
        viewTransition(() => item.classList.toggle("active"));
  })
});

tablis.forEach((li) => {
  li.addEventListener("click", (event) => {
    tablis.forEach((item) => {
      item.classList.remove("active");
    });
    event.target.classList.toggle("active");
    if (event.target.id == "all") {
      viewTransition(() => {
        for (let i = 0; i < galleryItems.length; i++) {
          galleryItems[i].style.display = "block";
        }
      });
    }
    if (event.target.id == "newYork") {
      viewTransition(() => showImages(imgNy, imgFlowers, imgOthers));
    }
    if (event.target.id == "flowers") {
      viewTransition(() => showImages(imgFlowers, imgNy, imgOthers));
    }

    if (event.target.id == "others") {
      viewTransition(() => showImages(imgOthers, imgFlowers, imgNy));
    }
  });
});


function viewTransition(fn, params) {
  if (document.startViewTransition) {
    document.startViewTransition(() => fn(params));
  } else {
    fn(params);
  }
}

function showImages(showImg, hideImg1, hideImg2) {
  for (let i = 0; i < showImg.length; i++) {
    showImg[i].style.display = "block";
  }
  for (let i = 0; i < hideImg1.length; i++) {
    hideImg1[i].style.display = "none";
  }
  for (let i = 0; i < hideImg2.length; i++) {
    hideImg2[i].style.display = "none";
  }
}

              
            
!
999px

Console