Scroll · 31 of 34

Color Morph

The background colour shifts smoothly as the block moves through the viewport. Scroll progress is mapped to an HSL hue and set as a CSS variable.

HTML · CSS · JavaScript · No dependencies · MIT

This one is driven by scroll position, so it reads best in context. See it on the scroll page.

HTML

<section class="color-morph-section">
  <h2>Background Shift</h2>
</section>

CSS

.color-morph-section {
  min-height: 35vh;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  transition: background 1s;
}

.color-morph-section h2 {
  font-size: 2.5rem;
  color: #f0f0f5;
}

.color-morph-section.in-view {
  background: rgba(124, 92, 255, 0.1);
}

JavaScript

var section = document.querySelector('.color-morph-section');

new IntersectionObserver(function (entries) {
  entries.forEach(function (entry) {
    entry.target.classList.toggle('in-view', entry.isIntersecting);
  });
}, { threshold: 0.25 }).observe(section);