【发布时间】:2020-02-05 16:17:25
【问题描述】:
我正在尝试复制this 功能,从而可以通过鼠标滚动来控制视频的进程。不同寻常的是,该功能在 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>
【问题讨论】:
-
虽然我发现很多关于这个功能的帖子,通常引用苹果的产品页面作为灵感,但似乎没有一个遇到这个特殊问题。我确实看到了一个 [stackoverflow.com/questions/41982520/html5-video-scroll-video],但我已经在 Chrome 上测试了 .mp4 版本,它的运行没有任何不同 - 而 Safari 运行流畅。所以我很确定这与视频的质量无关(假设 .mp4 视频的质量比 .webm 更好)
标签: javascript video scroll controls