HTML – 中心全屏背景视频

前端之家收集整理的这篇文章主要介绍了HTML – 中心全屏背景视频前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我最近玩过html5并且遇到了使用的想法

使用以下HTML代码作为网站背景的全屏视频:

  1. <video id = "video_background" preload = "auto" autoplay = "true" loop = "loop">
  2. <source src = "videos/football.mp4" type = "video/mp4" />
  3. </video>

另外,我使用以下css代码来正确对齐它:

  1. #video_background{
  2. position: absolute;
  3. bottom: 0px;
  4. right: 0px;
  5. min-width: 100%;
  6. min-height: 100%;
  7. max-width: 4000%;
  8. max-height:4000%;
  9. width: auto;
  10. height: auto;
  11. z-index: -1000;
  12. overflow: hidden;
  13. }

它工作得很好,但我希望我的视频水平居中
在每个浏览器分辨率.

无论我试图通过哪种方式实现这种效果(通过保证金或基于百分比的定位),
视频保持坚持正确的底部.

关于如何解决这个“问题”的任何想法?

解决方法

这是一个JQuery函数,我写了很长一段时间才使视频成为全屏背景.基本上,如果窗口的纵横比高于视频的纵横比,则使高度100%和宽度自动,反之亦然,以获得更宽的纵横比窗口.
  1. // Resize the video elements so that we don't get any borders due to aspect ratio
  2. function resizeVideo() {
  3. if ($(window).height() > $(window).width() * 0.5425) { // Which dimension is bigger dependant on aspect ratio (16:9)
  4. $("video").removeAttr("height").removeAttr("width").width("auto").height("100%");
  5. }
  6. else {
  7. $("video").removeAttr("height").removeAttr("width").width("100%").height("auto");
  8. }
  9. };
  10.  
  11.  
  12. // Add the resize function to the window resize event but put it on a short timer as to not call it too often
  13. var resizeTimer;
  14. $(window).resize(function () {
  15. clearTimeout(resizeTimer);
  16. resizeTimer = setTimeout(resizeVideo,150);
  17. });

猜你在找的HTML相关文章