<分区>
标签 javascript jquery html css video
如何让我的背景视频在播放结束后自动向下滚动?因此客户端可以在不接触鼠标的情况下自动查看下一步。什么是java脚本代码?
最佳答案
我制作这个 jsfiddle 是为了进行实验并为您提供一个具体示例 http://jsfiddle.net/www139/14mgca3q/ .我使变量名非常详细,以提供对代码正在做什么的切实理解。为了平滑滚动,使用 jquery animate with scrolling (just google it)。 这也是一个代码片段;)
window.addEventListener('load', function() {
var video = document.getElementById('video');
var element = document.getElementById('afterContent');
video.onended = function() {
//the video ended
//get the distance between the element and the top of the document.
var scrollDistance = document.body.scrollTop;
var elemRect = element.getBoundingClientRect();
var elemOffsetViewportDistance = elemRect.top;
var totalOffset = scrollDistance + elemOffsetViewportDistance;
window.scrollTo(0, totalOffset);
};
});
html,
body {
margin: 0;
padding: 0;
}
#afterContent {
position: relative;
top: 100vh;
}
<video id="video" tabindex="0" autobuffer="autobuffer" controls preload="preload">
<source type="video/webm; codecs="vp8, vorbis"" src="http://www.html5rocks.com/tutorials/video/basics/Chrome_ImF.webm"></source>
<source type="video/ogg; codecs="theora, vorbis"" src="http://www.html5rocks.com/tutorials/video/basics/Chrome_ImF.ogv"></source>
<source type="video/mp4; codecs="avc1.42E01E, mp4a.40.2"" src="http://www.html5rocks.com/tutorials/video/basics/Chrome_ImF.mp4"></source>
<p>Sorry, your browser does not support the <video> element.</p>
</video>
<div id="afterContent">
This is the div with the content.
</div>
我从@codelove 借来了很酷的视频(顺便说一句,非常酷的视频)。
关于用于视频滚动的 Javascript 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33839934/