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 id="progress"></div>
<main>
	<h1>Grasping CSS Scroll-Timeline <code>time-range</code></h1>
	<p>
		Given are a <label for="animation-duration">animation-duration</label> of <input type="range" id="animation-duration" min="0.1" max="2" value="1" step="0.1" /><output id="animation-duration-output">1s</output><br />
		and a <label for="time-range">time-range</label> of <input type="range" id="time-range" min="0.1" max="2" value="1" step="0.1" /><output id="time-range-output">1s</output>.
	</p>
	<p>
		When having scrolled for a distance of <input type="range" id="scroll-progress" min="0" max="100" value="0" /><output id="scroll-progress-output">0%</output><br />
		the clock of the scroll-timeline will be at <output id="animation-progress-output--time">0s</output><br />
		so our animation will be at 
		<output id="animation-progress-output--perc">0%</output></p>
</main>
              
            
!

CSS

              
                #progress {
	position: fixed;
	z-index: 10;
	left: 0;
	top: 0;
	width: 100vw;
	height: 1vh;
	transform-origin: 0 50%;
	transform: scaleX(0);
	background: red;
}

label, output, code {
	font-family: courier;
	background: #e4e4e4;
	font-size: 0.9em;
}

#animation-progress {
	background: #ffdd40;
}



/** NON ESSENTIAL STYLES BELOW **/
html,
body {
	margin: 0;
	padding: 0;
	background: #fff;
}

body {
	font-size: clamp(1.1em, calc(0.5em + 1vw), 3em);
	line-height: 1.42;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
}

main {
	font-weight: 300;
	width: 90vw;
	max-width: 40em;
	margin: 0 auto;
	padding: 4em 0;
}
              
            
!

JS

              
                const animation = new Animation(
    new KeyframeEffect(
      document.querySelector('#progress'),
      {
        transform: ['scaleX(0)', 'scaleX(1)'],
      },
      { duration: 1, fill: "both" }
    )
  );

// animation.play();


const $animationDuration = document.querySelector('#animation-duration');
const $animationDurationOutput = document.querySelector('#animation-duration-output');
const $timeRange = document.querySelector('#time-range');
const $timeRangeOutput = document.querySelector('#time-range-output');
const $scrollProgress = document.querySelector('#scroll-progress');
const $scrollProgressOutput = document.querySelector('#scroll-progress-output');
const $animationProgressOutputPerc = document.querySelector('#animation-progress-output--perc');
const $animationProgressOutputTime = document.querySelector('#animation-progress-output--time');

let animationDuration = parseFloat($animationDuration.value);
let timeRange = parseFloat($timeRange.value);
let scrollProgress = ($scrollProgress.value / 100);

const updateAnimation = () => {
	animation.currentTime = timeRange / animationDuration * scrollProgress;
};

const updateProgress = () => {
	$animationProgressOutputTime.innerHTML = `${parseFloat(timeRange * scrollProgress).toFixed(2)}s`;
	$animationProgressOutputPerc.innerHTML = `${parseInt(timeRange / animationDuration * scrollProgress * 100)}%`;
}

$scrollProgress.addEventListener('input', (e) => {
	$scrollProgressOutput.innerHTML = `${$scrollProgress.value}%`;
	scrollProgress = ($scrollProgress.value / 100);
	updateAnimation();
	updateProgress();
});

$timeRange.addEventListener('input', (e) => {
	$timeRangeOutput.innerHTML = `${$timeRange.value}s`;
	timeRange = parseFloat($timeRange.value);
	updateAnimation();
	updateProgress();
});

$animationDuration.addEventListener('input', (e) => {
	$animationDurationOutput.innerHTML = `${$animationDuration.value}s`;
	animationDuration = parseFloat($animationDuration.value);
	updateAnimation();
	updateProgress();
});
              
            
!
999px

Console