ComponentsMedia

Portfolio Carousel

Ready

A centered, looping portfolio slider after Pelicula — tall covers with display titles riding the image edge. Autoplays, drifts forever; click a side slide or drag to scrub.

Preview

Code

portfolio-carousel-example.html
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Bebas+Neue&display=swap" />
<style>
  .pc { width: 100%; font-family: Inter, system-ui, sans-serif; }
  .pc__viewport { overflow: hidden; cursor: grab; touch-action: pan-y; }
  .pc.is-dragging .pc__viewport { cursor: grabbing; }
  .pc__track {
    display: flex;
    gap: 40px;
    transition: transform 0.65s cubic-bezier(0.22, 0.61, 0.36, 1);
    will-change: transform;
  }
  .pc__slide { flex: 0 0 auto; width: 45%; margin: 0; user-select: none; -webkit-user-select: none; }
  .pc__link { display: block; overflow: hidden; }
  .pc__img {
    display: block;
    width: 100%;
    height: auto;
    aspect-ratio: 500 / 742;
    object-fit: cover;
    background: oklch(93% 0.008 252);
    transition: transform 0.5s cubic-bezier(0.22, 0.61, 0.36, 1);
  }
  .pc__slide:hover .pc__img { transform: rotate(-1.5deg) scale(1.05); }
  .pc__title {
    margin: -0.48em 0 0;
    font-family: "Bebas Neue", "Arial Narrow", system-ui, sans-serif;
    font-weight: 400;
    font-size: clamp(1.75rem, 3.2vw, 2.8rem);
    line-height: 0.96;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: #111;
    animation: pc-rise 0.7s cubic-bezier(0.16, 1, 0.3, 1) backwards;
    animation-delay: var(--pc-delay, 0s);
  }
  .pc__title a { color: inherit; text-decoration: none; transition: opacity 0.2s; }
  .pc__title a:hover { opacity: 0.55; }
  @keyframes pc-rise {
    from { opacity: 0; transform: translateY(12px); }
  }
  @media (prefers-reduced-motion: reduce) {
    .pc__track, .pc__img { transition: none; }
    .pc__title { animation: none; }
  }
</style>
<div class="pc" role="region" aria-roledescription="carousel" aria-label="Portfolio">
  <div class="pc__viewport">
    <div class="pc__track">
      <article class="pc__slide" role="group" aria-roledescription="slide" aria-label="1 of 5">
        <a class="pc__link" href="#">
          <img class="pc__img" src="cover-1.jpg" alt="Rio’s Dirt" width="500" height="742" loading="eager" />
        </a>
        <h3 class="pc__title"><a href="#">Rio’s Dirt</a></h3>
      </article>
      <article class="pc__slide" role="group" aria-roledescription="slide" aria-label="2 of 5">
        <a class="pc__link" href="#">
          <img class="pc__img" src="cover-2.jpg" alt="Mostro Atom" width="500" height="742" loading="lazy" />
        </a>
        <h3 class="pc__title"><a href="#">Mostro Atom</a></h3>
      </article>
      <article class="pc__slide" role="group" aria-roledescription="slide" aria-label="3 of 5">
        <a class="pc__link" href="#">
          <img class="pc__img" src="cover-3.jpg" alt="Big Bad Red" width="500" height="742" loading="lazy" />
        </a>
        <h3 class="pc__title"><a href="#">Big Bad Red</a></h3>
      </article>
      <article class="pc__slide" role="group" aria-roledescription="slide" aria-label="4 of 5">
        <a class="pc__link" href="#">
          <img class="pc__img" src="cover-4.jpg" alt="Hueco Mundo" width="500" height="742" loading="lazy" />
        </a>
        <h3 class="pc__title"><a href="#">Hueco Mundo</a></h3>
      </article>
      <article class="pc__slide" role="group" aria-roledescription="slide" aria-label="5 of 5">
        <a class="pc__link" href="#">
          <img class="pc__img" src="cover-5.jpg" alt="Call Me Ao" width="500" height="742" loading="lazy" />
        </a>
        <h3 class="pc__title"><a href="#">Call Me Ao</a></h3>
      </article>
    </div>
  </div>
</div>
<script>
  (function () {
    "use strict";

    var GAP = 40;        // gutter between slides, px
    var DUR = 650;       // slide transition, ms
    var INTERVAL = 3200; // autoplay tick, ms

    document.querySelectorAll(".pc").forEach(function (root) {
      var viewport = root.querySelector(".pc__viewport");
      var track = root.querySelector(".pc__track");
      var originals = Array.prototype.slice.call(track.children);
      var n = originals.length;
      var reduced = window.matchMedia("(prefers-reduced-motion: reduce)");
      var slideW = 0, step = 0, current = n;
      var timer = null, settleTimer = 0;
      var inView = true, hovering = false;
      var dragging = false, moved = false, suppressClick = false;
      var startX = 0, startTx = 0;

      function cloneOf(slide) {
        var clone = slide.cloneNode(true);
        clone.setAttribute("aria-hidden", "true");
        clone.querySelectorAll("a").forEach(function (a) { a.tabIndex = -1; });
        return clone;
      }
      var head = document.createDocumentFragment();
      var tail = document.createDocumentFragment();
      originals.forEach(function (slide) {
        head.appendChild(cloneOf(slide));
        tail.appendChild(cloneOf(slide));
      });
      track.appendChild(tail);
      track.insertBefore(head, track.firstChild);

      originals.forEach(function (slide, i) {
        slide.style.setProperty("--pc-delay", i * 90 + "ms");
      });

      function perViewFor(w) {
        if (w >= 1100) return 4;
        if (w >= 820) return 3;
        if (w >= 540) return 2.4;
        return 1.3;
      }

      function measure() {
        var vw = viewport.clientWidth;
        var perView = perViewFor(vw);
        slideW = (vw - GAP * (perView - 1)) / perView;
        step = slideW + GAP;
        Array.prototype.forEach.call(track.children, function (s) {
          s.style.width = slideW + "px";
        });
      }

      function offsetFor(i) {
        return (viewport.clientWidth - slideW) / 2 - i * step;
      }

      function mark() {
        Array.prototype.forEach.call(track.children, function (s, i) {
          s.classList.toggle("is-active", i === current);
        });
      }

      function paint(i) {
        track.style.transform = "translate3d(" + offsetFor(i) + "px, 0, 0)";
      }

      function jumpTo(i) {
        current = i;
        track.style.transition = "none";
        paint(i);
        void track.offsetWidth;
        track.style.transition = "";
        mark();
      }

      // Fold the index back into the middle copy once the move has played out.
      function settle() {
        if (current >= 2 * n) jumpTo(current - n);
        else if (current < n) jumpTo(current + n);
      }

      function goTo(i) {
        current = i;
        clearTimeout(settleTimer);
        if (reduced.matches) {
          jumpTo(i);
        } else {
          paint(i);
          settleTimer = setTimeout(settle, DUR + 80);
        }
        mark();
      }

      function start() {
        if (timer || reduced.matches || !inView || hovering || document.hidden) return;
        timer = setInterval(function () { goTo(current + 1); }, INTERVAL);
      }
      function stop() {
        clearInterval(timer);
        timer = null;
      }

      viewport.addEventListener("pointerdown", function (e) {
        if (e.pointerType === "mouse" && e.button !== 0) return;
        dragging = true;
        moved = false;
        startX = e.clientX;
        startTx = offsetFor(current);
        clearTimeout(settleTimer);
        track.style.transition = "none";
        root.classList.add("is-dragging");
        stop();
      });
      window.addEventListener("pointermove", function (e) {
        if (!dragging) return;
        var dx = e.clientX - startX;
        if (Math.abs(dx) > 6) moved = true;
        track.style.transform = "translate3d(" + (startTx + dx) + "px, 0, 0)";
      });
      window.addEventListener("pointerup", endDrag);
      window.addEventListener("pointercancel", endDrag);
      function endDrag(e) {
        if (!dragging) return;
        dragging = false;
        root.classList.remove("is-dragging");
        track.style.transition = "";
        var dx = (e.clientX || startX) - startX;
        var idx = Math.round(current - dx / step);
        if (Math.abs(dx) > slideW * 0.12 && idx === current) {
          idx = current + (dx < 0 ? 1 : -1);
        }
        goTo(idx);
        if (moved) {
          suppressClick = true;
          setTimeout(function () { suppressClick = false; }, 50);
        }
        start();
      }

      track.addEventListener("click", function (e) {
        if (suppressClick) { e.preventDefault(); return; }
        var slide = e.target.closest(".pc__slide");
        if (!slide) return;
        var link = e.target.closest("a");
        var idx = Array.prototype.indexOf.call(track.children, slide);
        if (idx !== current) {
          e.preventDefault();
          goTo(idx);
        } else if (link && link.getAttribute("href") === "#") {
          e.preventDefault();
        }
      });

      root.addEventListener("keydown", function (e) {
        if (e.key === "ArrowRight") { e.preventDefault(); goTo(current + 1); }
        else if (e.key === "ArrowLeft") { e.preventDefault(); goTo(current - 1); }
      });

      root.addEventListener("dragstart", function (e) { e.preventDefault(); });
      root.addEventListener("pointerenter", function () { hovering = true; stop(); });
      root.addEventListener("pointerleave", function () { hovering = false; start(); });
      root.addEventListener("focusin", stop);
      root.addEventListener("focusout", start);
      document.addEventListener("visibilitychange", function () {
        if (document.hidden) stop(); else start();
      });
      if ("IntersectionObserver" in window) {
        new IntersectionObserver(function (entries) {
          inView = entries[0].isIntersecting;
          if (inView) start(); else stop();
        }, { threshold: 0.25 }).observe(root);
      }

      var raf = 0;
      window.addEventListener("resize", function () {
        cancelAnimationFrame(raf);
        raf = requestAnimationFrame(function () {
          measure();
          jumpTo(current);
        });
      });

      measure();
      jumpTo(current);
      start();
    });
  })();
</script>