html5的video的封面poster图片填充

html5支持视频播放,并且趋势,facebook也全面切换到html5了,最近作一个简单的视频播放器,测试了好多jplayer,video.js之类的都以为不太好,因此本身写一个最简单的,不过发现了一个问题,视频播放以前的封面不太好……css

封面的尺寸被强制缩小了,我须要填充整个播放器的。html

< !DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <title></title>
        <style type="text/css">
        .video-container1{
            width: 400px;
            height: 400px;
            border: solid;
        }
        .video1{
            width: 100%;
            height: 100%;
        }
        </style>
    </head>
    <body>
    <div class="video-container1">
        <video class="video1" src="oceans.mp4" poster="1.jpg" controls>

        </video>
    </div>
    </body>
</html>

blog_html5_video_background_01_20151223.jpg
blog_html5_video_background_02_20151223.jpg

因此查了一下资料,并无发现html5的video属性支持处理poster的尺寸问题,而后发现了一个hacker的方法:
The answer is actually quite simple. Instead of providing our poster image as a value to the poster attribute, we define it as a background image for our video element, and use the background-size property to tell the browser that the image is to cover the element in question:html5

将poster页面设置为一个透明的图片或者不存在的值,这样浏览器就会没法显示poster,而后经过设置播放器的css背景background,将咱们须要的背景图放进去,而且填充背景,而且咱们用background-size属性去告诉浏览器,这个播放器或者这个元素被这个图片覆盖。git

video{
    width: 100%;
    height: 100%;
    background:transparent url('img/1.jpg') 50% 50% no-repeat;

    //下面就是background-size,每种浏览器都写一个配置
    -webkit-background-size:cover;
    -moz-background-size:cover;
    -o-background-size:cover;
    background-size:cover;
}

详细代码在这里:
友人提醒,将测试代码放到github方便其余人测试:(https://github.com/yuanyuanyuan/my-git/tree/master/test1github

< !DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <title></title>
        <style type="text/css">
        .video-container1{
            width: 400px;
            height: 400px;
            border: solid;
        }
        .video1{
            width: 100%;
            height: 100%;
        }
        .video-container2{
            width: 400px;
            height: 400px;
            border: solid;
        }
        .video2{
            width: 100%;
            height: 100%;
            background:transparent url('1.jpg') 50% 50% no-repeat;
            -webkit-background-size:cover;
            -moz-background-size:cover;
            -o-background-size:cover;
            background-size:cover;
        }
        </style>
    </head>
    <body>
    <div class="video-container1">
        <video class="video1" src="oceans.mp4" poster="1.jpg" controls>

        </video>
    </div>
    <div class="video-container2">
        <video class="video2" src="oceans.mp4" poster="2.jpg" controls>

        </video>
    </div>
    </body>
</html>

展现效果以下:web

blog_html5_video_background_03_20151223.jpg

引用参考:
http://www.iandevlin.com/blog/2013/03/html5/html5-video-and-background-images
http://www.w3school.com.cn/cssref/pr_background.asp
http://www.w3schools.com/css/css_image_transparency.asp浏览器

相关文章
相关标签/搜索