我正在尝试复制此功能,从而可以通过鼠标滚动来控制视频的进度。不同寻常的是,该功能在Safari上完美运行,但在 Firefox、Opera 和 Brave 上却无法正常运行 - 在 Chrome 上,视频更新似乎被阻止,直到整个滚动事件完成,但它确实控制了视频的位置。因此,它给人的印象是非常锯齿或滞后(因此可以在 Chrome 上滚动视频而完全不移动)。我有一种感觉,我错过了一些与requestAnimationFrame或明显有关的东西Intersection Observer。请好心人看看,如果是这样,请告诉我,如果是这样,在哪里?
<div id="set-height">
<video width="100%" height="auto" id="v0">
<source src="Video.webm" type="video/webm"></source>
<source src="Video.mp4" type="video/mp4"></source>
</video>
</div>
<script>
const playbackConst = 150, // lower the number, quicker the animation
waveVid = document.querySelector('.index-section--scroll-video'),
vid = document.getElementById('v0');
let frameNumber = 0,
myRequest = window.requestAnimationFrame(scrollPlay);
function scrollPlay() {
frameNumber = ((window.innerHeight * 0.7) - vid.getBoundingClientRect().top) / playbackConst; // position of where the animation starts: 100vh - distance to video
vid.currentTime = frameNumber;
window.requestAnimationFrame(scrollPlay); // recursive call is necessary for functionality
}
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (!entry.isIntersecting) {
window.cancelAnimationFrame(myRequest); // this was needed as Firefox was eating my CPU up on the site
return;
} else {
window.requestAnimationFrame(scrollPlay);
console.log(vid.getBoundingClientRect().top);
}
});
observer.observe(vid);
});
</script>