<main>
  <section>
    <img src='https://images.unsplash.com/photo-1541447271487-09612b3f49f7?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2OTAwOTk3NjN8&ixlib=rb-4.0.3&q=85' alt=''>
    <div class="text">
      <h2>I just love</h2>
      <p>These scroll driven animations</p>
    </div>
  </section>
  <section>
    <img src='https://images.unsplash.com/photo-1548248823-ce16a73b6d49?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2OTAwOTk3NjN8&ixlib=rb-4.0.3&q=85' alt=''>
    <div class="text">
      <h2>And the fact that</h2>
      <p>combined with scroll-snap</p>
    </div>
  </section>
  <section>
    <img src='https://images.unsplash.com/photo-1502462456379-a6dbfdd0faef?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2OTAwOTk3NjN8&ixlib=rb-4.0.3&q=85' alt=''>
    <div class="text">
      <h2>These can create some cool effects</h2>
      <p>When entering or leaving the scroll view</p>
    </div>
  </section>
  <section>
    <img src='https://images.unsplash.com/photo-1600540984005-c7f3a641fbe5?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2OTAwOTk3NjN8&ixlib=rb-4.0.3&q=85' alt=''>
    <div class="text">
      <h2>I really think this opens a lot of possibilities</h2>
      <p>For a performant web, with a bit of</p>
    </div>
  </section>
  <section>
    <img src='https://images.unsplash.com/photo-1472148083604-64f1084980b9?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2OTAwOTk3NjN8&ixlib=rb-4.0.3&q=85' alt=''>
    <div class="text">
      <h2>Extra ooooomph</h2>
      <p>(yes, i'm running a bit out of inspiration here)</p>
    </div>
  </section>
  <section>
    <img src='https://images.unsplash.com/photo-1598818384697-62330d600309?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2OTAxMDAxNDd8&ixlib=rb-4.0.3&q=85' alt=''>
    <div class="text">
      <h2>These are some pretty architecture images right?</h2>
      <p>Just used unsplash for them</p>
    </div>
  </section>
  <section>
    <img src='https://images.unsplash.com/photo-1553448539-a13a5595a494?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2OTAxMDAxNDd8&ixlib=rb-4.0.3&q=85' alt=''>
    <div class="text">
      <h2>Time for me to stop adding some random stuff here</h2>
      <p>"Lorem ipsum dolor sit amet" could work just as well</p>
    </div>
  </section>
  <section>
    <img src='https://images.unsplash.com/photo-1506438481471-719be21dadee?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2OTAxMDAxNDd8&ixlib=rb-4.0.3&q=85' alt=''>
    <div class="text">
      <h2>For the love of Scroll Driven Animations</h2>
      <p>and a bit for scroll-snap as well</p>
    </div>
  </section>
</main>
@import url("https://fonts.googleapis.com/css2?family=Architects+Daughter&family=Open+Sans&display=swap");

@layer base, snapping, keyframes;

@layer keyframes {
  /* The keyframes */
  @keyframes inAndOut {
    entry 0% {
      opacity: 0;
      transform: translateX(-50%);
    }
    entry 100% {
      opacity: 1;
      transform: translateX(0);
    }

    exit 0% {
      opacity: 1;
      transform: translateX(0);
    }
    exit 100% {
      opacity: 0;
      transform: translateX(-50%);
    }
  }

  @keyframes inAndOutReverse {
    entry 0% {
      opacity: 0;
      transform: translateX(50%);
    }
    entry 100% {
      opacity: 1;
      transform: translateX(0);
    }

    exit 0% {
      opacity: 1;
      transform: translateX(0);
    }
    exit 100% {
      opacity: 0;
      transform: translateX(50%);
    }
  }

  /* The vertical keyframes */
  @keyframes inAndOutVertical {
    entry 0% {
      opacity: 0;
      transform: translateY(50%);
    }
    entry 100% {
      opacity: 1;
      transform: translateY(0);
    }

    exit 0% {
      opacity: 1;
      transform: translateY(0);
    }
    exit 100% {
      opacity: 0;
      transform: translateY(-50%);
    }
  }
}

@layer snapping {
  main {
    width: 100%;
    height: 100vh;
    overflow-y: scroll;
    overflow-x: hidden;
    scroll-snap-type: y mandatory;
    & section {
      scroll-snap-align: start;
    }
  }

  img {
    flex: 0 0 50%;
    width: 50%;
    height: 100%;
    object-fit: cover;
    object-position: center;

    animation: linear inAndOut;
    animation-timeline: view();
  }

  section {
    display: flex;
    align-items: center;
    min-height: 300px;
    height: 50vh;
    &:nth-child(even) {
      background: black;
      color: white;
      flex-direction: row-reverse;
      & img {
        animation-name: inAndOutReverse;
      }
    }
  }

  .text {
    padding: 4rem;
    flex: 0 0 50vw;
    animation: linear inAndOutVertical;
    animation-timeline: view();
  }

  @media (width <= 700px) {
    section {
      height: 100dvh;
      align-items: stretch;
      flex-wrap: wrap;
      & img,
      .text {
        flex: 0 0 100%;
        animation-name: inAndOutVertical;
      }
    }
    img {
      height: 60dvh;
      flex-grow: 1;
    }
    .text {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 40dvh;
      padding: 0.6rem;
    }
  }
}

@layer base {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  body {
    font-family: "Open Sans", sans-serif;
    font-size: 1.2rem;
    margin: 0;
    padding: 0;
  }

  h2 {
    font-family: "Architects Daughter", cursive;
    font-size: 2rem;
    letter-spacing: 0.05em;
    margin: 0 0 1rem;
  }
}
Run Pen

External CSS

  1. https://codepen.io/utilitybend/pen/oNabmYR.css

External JavaScript

  1. https://codepen.io/utilitybend/pen/oNabmYR.js