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

jQuery version: https://jsfiddle.net/xaliber/9dLyskmc/1/

by xaliber

HTML

<div class="container">

  <div class="top" id="top">
    <div class="title" id="title">
      Fade Away
    </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 thought leader, overcome injustice, expose the truth collective...

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: #aaa;
  height: 300px;
  opacity: 1;
  text-align: center;
  font-family: 'helvetica';
  font-size: 100px;
  font-weight: 700;
  color: #fff;
}

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

/* 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 myDiv = document.getElementsByClassName('title')[0];

// 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() {

  myDiv.style.opacity = 1-((parallaxWindow.scrollTop)/150);
  myDiv.style.filter = 'alpha(opacity=' + 1-((parallaxWindow.scrollTop)/150) + ')';
  
  /* useless
  
  if (parallaxWindow.scrollTop < 100) {
    myDiv.style.opacity = 1;
  } else if (parallaxWindow.scrollTop < 125) {
    myDiv.style.opacity = 0.75;
  } else if (parallaxWindow.scrollTop < 150) {
    myDiv.style.opacity = 0.5;
  } else if (parallaxWindow.scrollTop < 175) {
    myDiv.style.opacity = 0.25;
  } else {
    myDiv.style.opacity = 0;
  }
  */

}

parallaxWindow.addEventListener('scroll', scrolled);