<div id="videoContainer">
<video id="myVideo">
<source src="https://assets.codepen.io/39255/output_960.mp4" type="video/mp4" />
</video>
</div>
* {
margin: 0;
padding: 0;
}
#videoContainer {
height: 100vh;
overflow: visible;
}
#myVideo {
position: fixed;
top: 0;
left: 0;
width: 100%;
}
// get video element
const video = document.getElementById("myVideo");
const container = document.getElementById("videoContainer");
// set the container height according to video length
video.addEventListener('loadedmetadata', function() {
const speed = 250; // can be any number (adjust to your preference)
container.style.height = (video.duration * speed) + 'px';
});
// play video using scroll values
// function is attached to scroll event.
const playVideo = () => {
// get current scroll progress
var scrollY = window.scrollY;
// get total page height and calculate percentage
var height = document.documentElement.scrollHeight - window.innerHeight;
var percentage = (scrollY / height);
// set video playback position.
video.currentTime = video.duration * percentage;
window.requestAnimationFrame(playVideo);
};
window.addEventListener("scroll", playVideo);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.