Created
July 5, 2023 17:56
-
-
Save fongreecss/69dd3ce2cdc7123161cf4cf1d1f219e5 to your computer and use it in GitHub Desktop.
Video play on scroll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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