欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-15 14:26:13
html5 video全屏播放/自动播放的实现示例这篇文章主要介绍了html5 video全屏播放/自动播放的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 20-08-06...

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