<main>
  <div id="instructions">
    Scroll the page to make the element change color when it passes the scrollOffset point.
  </div>
  <div class="vh-container">
  <div class="vh-line">
  </div>
    <p class="vh-display">
      window.innerHeight = <span id="window-height"></span>px
    </p>
  </div>
  <div class="scroll-offset">
    scrollOffset = 100px
  </div>
  <div class="element-container">
    <div class="js-scroll">
      <p>element.getBoundingClientRect().top = <span id="offset-top"></span>px</p>
    </div>
  </div>
</main>
@import url("https://fonts.googleapis.com/css2?family=Lato&display=swap");

:root {
  --offset-height: 100px;
}

body {
  margin: 0;
}

#instructions {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  width: 50%;
  max-width: 300px;
  height: fit-content;
  margin: auto;
  text-align: center;
  font-weight: bold;
  font-family: "Lato", sans-serif;
  transition: opacity 500ms;
}

#instructions.hidden {
  opacity: 0;
}

.vh-container {
  position: relative;
}

.vh-line {
  height: 97vh;
  top: 1.5vh;
  width: 2px;
  margin-left: 5px;
  position: fixed;
  background-color: black;
}

.vh-line:before {
  position: fixed;
  content: "";
  top: 0;
  left: 1px;
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 15px solid black;
}

.vh-line:after {
  position: fixed;
  content: "";
  bottom: 0;
  left: 1px;
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 15px solid black;
}

.vh-display {
  position: fixed;
  height: max-content;
  top: 50%;
  bottom: 50%;
  transform-origin: left;
  left: 25px;
  margin: auto;
  transform: rotateZ(-90deg) translateX(-50%);
}

#window-height,
#offset-top {
  color: red;
}

.scroll-offset {
  text-align: right;
  position: fixed;
  bottom: 0;
  width: 100%;
  height: var(--offset-height);
  border-top: 1px dashed red;
}

.element-container {
  margin-top: 97vh;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.element-container p {
  margin: 0;
}

.js-scroll {
  width: 50%;
  height: 300px;
  min-width: 300px;
  background-color: #dadada;
  transition: background-color 500ms;
}

.js-scroll.scrolled {
  background-color: #efd3d4;
}
const scrollOffset = 100;

const windowHeight = document.getElementById("window-height");
const offsetTop = document.getElementById("offset-top");
const scrollElement = document.querySelector(".js-scroll");
const instructions = document.getElementById("instructions");

const getWindowHeight = () => {
  windowHeight.innerHTML =
    window.innerHeight || document.documentElement.clientHeight;
};

const getOffsetTop = () => {
  offsetTop.innerHTML = scrollElement.getBoundingClientRect().top;
}

const elementInView = (el, offset = 0) => {
  const elementTop = el.getBoundingClientRect().top;

  return (
    elementTop <= 
    ((window.innerHeight || document.documentElement.clientHeight) - offset)
  );
};

const displayScrollElement = () => {
  scrollElement.classList.add('scrolled');
  instructions.classList.add('hidden');
}

const hideScrollElement = () => {
  scrollElement.classList.remove('scrolled');
  instructions.classList.remove('hidden');
}

const handleScrollAnimation = () => {
  if (elementInView(scrollElement, scrollOffset)) {
      displayScrollElement(scrollElement);
  } else {
    hideScrollElement(scrollElement);
  }
}

getWindowHeight();
getOffsetTop()

window.addEventListener('resize', () => {
  getWindowHeight()
})

window.addEventListener('scroll', () => {
  getOffsetTop();
  handleScrollAnimation();
})

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.