没有黑色边框的全屏视频

时间:2013-06-07 12:37:15

标签: javascript jquery html css

我遇到的问题是视频的侧面或顶部/底部总是会出现黑条,具体取决于屏幕尺寸。

enter image description here

任何想法如何在不显示恼人的黑条的情况下全屏显示?并且不使用插件。

这是我的标记:

    <div id="full-bg">
        <div class="box iframe-box" width="1280" height="800">
            <iframe src="http://player.vimeo.com/video/67794477?title=0&amp;byline=0&amp;portrait=0&amp;color=0fb0d4" width="1280" height="720" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
        </div>
    </div>
    #full-bg{
        width: 100%;
        height: 100%;
        img{
            display: none;
        }
        .iframe-box{
            width: 100%;
            height: 100%;
            position: absolute;
            background: url(../img/fittobox.png);
            left: 0 !important;
            top: 0 !important;
            iframe{
                width: 100%;
                height: 100%;
            }
        }
    }

3 个答案:

答案 0 :(得分:6)

尝试添加到您的CSS

.iframe-box {
    max-width: 1280px; /* video width */
    max-height: 720px; /* video height */
}

这意味着width: 100%; height: 100%会让元素尽可能多地扩展,直到它分别达到720px1280px的最大高度或宽度。

如果您正在查看的屏幕具有更高的分辨率,则该节点将停止展开,您将不会有黑色边框。


此外,afaik以下是无效的CSS,您使用的是库还是什么?

#full-bg {
    .iframe-box {
        foo: bar;
    }
}

接受回答后编辑:我只是想到了一种完全不同的方法来实现这一点,但它需要你改变很多CSS

.fittobox {                /* give fit to box an aspect ratio */
    display: inline-block; /* let it be styled thusly */
    padding: 0;            /* get rid of pre-styling */
    margin: 0;
    width: 100%;           /* take up full width available */
    padding-top: 56.25%;   /* give aspect ratio of 16:9; "720 / 1280 = 0.5625" */
    height: 0px;           /* don't want it to expand beyond padding */
    position: relative;    /* allow for absolute positioning of child elements */
}

.fittobox > iframe {
    position: absolute;    /* expand to fill */
    top: 0px;
    left: 0px;
    right: 0px;
    bottom: 0px;
}

答案 1 :(得分:1)

如果您知道视频的宽高比,则甚至不需要Javascript。您可以使用基于百分比的padding-top

我可以发布代码,但无论如何我建议你阅读this entire article

答案 2 :(得分:0)

@Paul S.的答案适用于.fittobox容器(视频纵横比为16:9),但是.fittobox > iframe嵌入的CSS上仍然带有黑条。删除右边和底部的位置对我来说就可以解决(当然,在那些0值中不需要“ px”):

.fittobox > iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}