<h2 class="heading">Hello Beautiful World!!</h2>
<h2 class="heading">恐るべきスピード!!!</h2>

<p class="scroll">↓ スクロール ↓</p>
.heading {
  color: #0bd;
  font-size: 4rem;
  font-weight: bold;
  margin: 600px 0;
  opacity: 0;
  transform: translateX(-100px);
}
.active {
  opacity: 1;
  transition: 1s;
  transform: translateX(0);
}

.scroll {
  text-align: center;
  position: fixed;
  top: 0;
  width: 100%;
}
// 着火点となる要素
const headings = document.querySelectorAll('.heading');

const options = {
  threshold: 1
};

// 実行するよ
const observer = new IntersectionObserver(showElements);

// 各 .heading に到達したら発動。複数あるから forEach 使うよ。
headings.forEach(heading => {
  observer.observe(heading);
});

// 要素が表示されたら実行する動作
function showElements(entries){
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      // 各 .heading に .active を加える
      entry.target.classList.add('active');
    }
  });
};
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.