Skip to content

Instantly share code, notes, and snippets.

@fongreecss
Created July 5, 2023 17:56
Show Gist options
  • Save fongreecss/69dd3ce2cdc7123161cf4cf1d1f219e5 to your computer and use it in GitHub Desktop.
Save fongreecss/69dd3ce2cdc7123161cf4cf1d1f219e5 to your computer and use it in GitHub Desktop.
Video play on scroll
var video = document.getElementById('hero_video');
var video_wrapper = document.getElementById('mc-hero');
var videoStart = video_wrapper.offsetTop;
var target = 0,
current = 0,
easeAmount = 0.05;
function scrollVideo() {
var videoLength = video.duration,
scrollPosition = document.documentElement.scrollTop || document.body.scrollTop;
if (scrollPosition >= videoStart) {
target = ((scrollPosition - videoStart) / (8289 - window.innerHeight)) * videoLength;
}
}
function ease(current, target, easeAmount) {
return current + (target - current) * easeAmount;
}
function animate() {
current = ease(current, target, easeAmount);
if(video.readyState >= video.HAVE_FUTURE_DATA) {
video.currentTime = current;
}
requestAnimationFrame(animate);
}
window.onscroll = function() {
scrollVideo();
};
animate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment