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

2023-12-02 0 283

近期开始开发公司新版官网, 首页顶部(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全屏播放/自动播放内容请搜索悠久资源网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持悠久资源网!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 HTML5 html5 video全屏播放/自动播放的实现示例 https://www.u-9.cn/sheji/html5/26401.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务