Parallax scrolling with multiple divs fade out on scroll (CSS-only + pure JS)

Original (only one element): https://jsfiddle.net/xaliber/g81bh5r7/

by xaliber

HTML

<div class="container">

  <div class="top" id="top">
    <div class="title fade" id="title">
      Fade Away
    </div>
    <div class="span fade">
      Subtitle
    </div>
  </div>

  <div class="content">
    <p>
      <a href="https://jsfiddle.net/xaliber/9dLyskmc/1/">Easier solution with jQuery.</a>
    </p>
    <p>
      Both in jQuery and here, what's scrolling is actually .content, not body.
    </p>
    <p>
      White paper segmentation families granular big data dynamic natural resources energize, vibrant families social return on investment human-centered radical. Inclusive philanthropy design thinking agile, we must stand up ecosystem; support social impact
      efficient game-changer ecosystem and correlation shared value. Ideate state of play technology circular, disrupt innovation paradigm movements change-makers. Natural resources triple bottom line bandwidth movements venture philanthropy incubator
      energize effective problem-solvers uplift. Greenwashing we must stand up segmentation, program area; resilient venture philanthropy, academic expose the truth entrepreneur activate transparent venture philanthropy empower communities deep dive.
      NGO collective impact synergy initiative, bandwidth, storytelling revolutionary inspiring, our work segmentation.
    </p>
    <p>
      Deep dive the resistance, problem-solvers impact investing social entrepreneur indicators inspiring energize. Boots on the ground our work systems thinking think tank innovation. Green space catalyze blended value sustainable empower communities thought
      partnership.

    </p>
    <p>
      NGO disrupt, expose the truth save the world, black lives matter challenges and opportunities thought leader movements efficient theory of change cultivate activate strategy LGBTQ+. Paradigm; emerging because, a; social enterprise strategy accessibility.
      Design thinking, segmentation; relief, justice her body her rights gender rights. Catalyze parse inclusion...

CSS

body {
  margin: 0;
  height: 1000px;
  font-family: sans-serif;
  color: #333;
}

.content {
  padding: 10%;
}

p {
  line-height: 1.75;
}

.top {
  margin: 0;
  position: relative;
  width: 100%;
  background-color: #7558c9;
  height: 300px;
  opacity: 1;
  text-align: center;
  font-family: 'helvetica';
  color: #fff;
}

.title {
  font-size: 100px;
  font-weight: 700;
  position: absolute;
  top: 60%;
  left: 100px;
  /* gak perlu, udah cukup smooth
  transition: opacity 0.5s ease;
  */
}

.span {
  font-size: 50px;
  font-weight: 300;
  position: absolute;
  top: 77%;
  right: 100px;
}

/* Start parallax, to disable just comment this and change the JS to "window" instead of "parallaxWindow" */

html,
body {
  scroll-behavior: smooth;
  overflow: hidden;
}

.container {
  perspective: 1px;
  margin-left: -3px;
  transform-style: preserve-3d;
  height: 100vh;
  overflow-x: hidden;
  overflow-y: scroll;
}

.top {
  display: flex;
  flex: 1 0 auto;
  position: relative;
  z-index: -1;
  height: 100vh;
  justify-content: center;
  align-items: center;
  transform: translateZ(-1px) scale(2);
}

.content {
  background: rgba(255, 255, 255, .5);
  position: relative;
  z-index: 1;
}

JavaScript

var fadeDiv = document.getElementsByClassName('fade');

// To make this compatible with parallax, switch "window" to "parallaxWindow" and "pageYOffset" to "scrollTop" (adjust the height too)

var parallaxWindow = document.getElementsByClassName('container')[0];

// parallaxWindow is obviously added for this purpose

function scrolled() {

  for (var i = 0; i < fadeDiv.length; i++) {
    var fadeDivParent = fadeDiv[i].parentElement.scrollHeight;
    
    fadeDiv[i].style.opacity = 1 - ((parallaxWindow.scrollTop) / (fadeDivParent / 2));
    
    fadeDiv[i].style.filter = 'alpha(opacity=' + 1 - ((parallaxWindow.scrollTop) / (fadeDivParent / 2)) + ')';
  }



}

parallaxWindow.addEventListener('scroll', scrolled);