html5 video全屏播放/自动播放的实现示例分享!

近期开始开发公司新版官网, 首页顶部(header)是一个全屏播放的小视频, 现简单总结如下:

页面代码

  <header class="header" style="width:100%;position: relative;">      <?php if(!Helper::isMobile()) { ?>      <video id="homeVideo" class="home-video" autoplay loop muted poster="res/video/cover.jpg">          <source src="res/video/home_video.mp4" type="video/mp4">      </video>      <?php } ?>  </header>  

其中php简单判断了一下是否是移动设备, 移动设备不展示视频(如果移动端展示的话, 需要解决iOS上无法自动播放的问题):

ps: 如果H5页面主要在微信浏览器中访问,可以解决iOS上视频自动播放的问题:解决iOS h5 audio自动播放(亲测有效)

  class Helper {      public static function isMobile() {          if (preg_match("/(iPhone|iPod|Android|ios|iPad)/i", $_SERVER['HTTP_USER_AGENT'])) {              return true;          } else {              return false;          }      }  }  

video标签样式

为了让视频占满整个屏幕, 关键在于video标签样式的设置:

  .home-video {      z-index: 100;      position: absolute;      top: 50%;      left: 50%;      min-width: 100%;      min-height: 100%;      object-fit: fill;/*这里是关键*/      width: auto;      height: auto;      -ms-transform: translateX(-50%) translateY(-50%);      -webkit-transform: translateX(-50%) translateY(-50%);      transform: translateX(-50%) translateY(-50%);      background: url(../video/cover.jpg) no-repeat;      background-size: cover;  }

视频跟随浏览器窗口大小的改变:

  $('.home-video').height(window.innerHeight);  $('.header').height(window.innerHeight);  $(window).resize(function() {      $('.home-video').attr('height', window.innerHeight);      $('.home-video').attr('width', window.innerWidth);      $('.header').height(window.innerHeight);  });  

页面加载完成再次触发播放,防止autoplay未生效

  document.getElementById('homeVideo').play();

到此这篇关于html5 video全屏播放/自动播放的实现示例的文章就介绍到这了,更多相关html5 video全屏播放/自动播放内容请搜索<计算机技术网>以前的文章或继续浏览下面的相关文章,希望大家以后多多支持<计算机技术网>!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ctvol.com/htm5ctutorials/461582.html

(0)
上一篇 2020年10月27日
下一篇 2020年10月27日

精彩推荐