Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="inner">
  <h2>Scroll me</h2>
  <p>
    Code based on CSS-tricks article &quot;<a href="https://css-tricks.com/styling-based-on-scroll-position/">Styling Based on Scroll Position</a>&quot;.
  </p>
  <p>
    Using css var to dynamically change css values. In this case the hue inside hsl() is changed.
  </p>
  <p>
    Other fun properties to use this on are opacity and transforms. For example used as an alternative method for parallax.
  </p>
</div>
              
            
!

CSS

              
                :root {
  --scrollpos: 0;
}
body {
  min-height: 600vh;
  background-color: hsl(calc(var(--scrollpos) * 3.6), 100%, 50%);
}
.inner {
  position: fixed;
}
              
            
!

JS

              
                // The debounce function receives our function as a parameter
const debounce = (fn) => {

  // This holds the requestAnimationFrame reference, so we can cancel it if we wish
  let frame;

  // The debounce function returns a new function that can receive a variable number of arguments
  return (...params) => {
    
    // If the frame variable has been defined, clear it now, and queue for next frame
    if (frame) { 
      cancelAnimationFrame(frame);
    }

    // Queue our function call for the next frame
    frame = requestAnimationFrame(() => {
      
      // Call our function and pass any params we received
      fn(...params);
    });

  } 
};


// Reads out the scroll position and stores it in the data attribute
// so we can use it in our stylesheets
const storeScroll = () => {
  let perc = window.scrollY / (document.body.clientHeight - window.innerHeight) * 100;
  document.documentElement.setAttribute('style', `--scrollpos: ${perc}`);
  
}

// Listen for new scroll events, here we debounce our `storeScroll` function
document.addEventListener('scroll', debounce(storeScroll), { passive: true });

// Update scroll position for first time
storeScroll();
              
            
!
999px

Console