用于视频滚动的 Javascript 代码

标签 javascript jquery html css video

<分区>


想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post .

关闭 7 年前

如何让我的背景视频在播放结束后自动向下滚动?因此客户端可以在不接触鼠标的情况下自动查看下一步。什么是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=&quot;vp8, vorbis&quot;" src="http://www.html5rocks.com/tutorials/video/basics/Chrome_ImF.webm"></source>
  <source type="video/ogg; codecs=&quot;theora, vorbis&quot;" src="http://www.html5rocks.com/tutorials/video/basics/Chrome_ImF.ogv"></source>
  <source type="video/mp4; codecs=&quot;avc1.42E01E, mp4a.40.2&quot;" src="http://www.html5rocks.com/tutorials/video/basics/Chrome_ImF.mp4"></source>
  <p>Sorry, your browser does not support the &lt;video&gt; element.</p>
</video>
<div id="afterContent">
  This is the div with the content.
</div>

我从@codelove 借来了很酷的视频(顺便说一句,非常酷的视频)。

关于用于视频滚动的 Javascript 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33839934/

上一篇:javascript - 如何在不同页面上使不同的页脚背景颜色与每个背景颜色相同

下一篇:css - rails 4 x SASS : load specific stylesheet in Internet Explorer

相关文章:

javascript - 从 mojolicious 调用 javascript href

php - Javascript 字符串小写以匹配数据库字段

html - 在使用 css 调整大小时使元素以图像为中心

javascript - 将 JavaScript 变量的值设置为 PHP 变量

javascript - 为什么onClick会导致循环?

javascript - 如何使用 Play Framework 和 jQuery 上传和显示文件?

javascript - jQuery Ajax .ajaxSuccess() 事件未触发

Javascript删除数组中的连接项

javascript - 如何在 Angularjs 中的 ng-show 之后操作 DOM?

javascript - jQuery - 记住触发函数