【问题标题】:Google chrome showing black edges when playing video谷歌浏览器在播放视频时显示黑边
【发布时间】:2016-06-28 00:44:56
【问题描述】:

我添加了一个视频背景,除了 Google Chrome 和 Firefox 在我播放视频时显示黑边之外,一切正常(见图)。当我在 Edge 和 Internet Explorer 上运行页面时,边缘没有出现。

编辑:我知道黑色边缘是因为我将容器作为黑色背景,但我不希望容器出现在边缘。我只需要全幅显示视频

$( document ).ready(function() {

    // Resive video
    scaleVideoContainer();

    initBannerVideoSize('.video-container .poster img');
    initBannerVideoSize('.video-container .filter');
    initBannerVideoSize('.video-container video');
        
    $(window).on('resize', function() {
        scaleVideoContainer();
        scaleBannerVideoSize('.video-container .poster img');
        scaleBannerVideoSize('.video-container .filter');
        scaleBannerVideoSize('.video-container video');
    });

});

/** Reusable Functions **/
/********************************************************************/

function scaleVideoContainer() {

    var height = $(window).height();
    var unitHeight = parseInt(height) + 'px';
    $('.homepage-hero-module').css('height',unitHeight);

}

function initBannerVideoSize(element){
    
    $(element).each(function(){
        $(this).data('height', $(this).height());
        $(this).data('width', $(this).width());
    });

    scaleBannerVideoSize(element);

}

function scaleBannerVideoSize(element){

    var windowWidth = $(window).width(),
        windowHeight = $(window).height(),
        videoWidth,
        videoHeight;
    
    console.log(windowHeight);

    $(element).each(function(){
        var videoAspectRatio = $(this).data('height')/$(this).data('width'),
            windowAspectRatio = windowHeight/windowWidth;

        if (videoAspectRatio > windowAspectRatio) {
            videoWidth = windowWidth;
            videoHeight = videoWidth * videoAspectRatio;
            $(this).css({'top' : -(videoHeight - windowHeight) / 2 + 'px', 'margin-left' : 0});
        } else {
            videoHeight = windowHeight;
            videoWidth = videoHeight / videoAspectRatio;
            $(this).css({'margin-top' : 0, 'margin-left' : -(videoWidth - windowWidth) / 2 + 'px'});
        }

        $(this).width(videoWidth).height(videoHeight);

        $('.homepage-hero-module .video-container video').addClass('fadeIn animated');
        

    });
}
.homepage-hero-module {
  border-right: none;
  border-left: none;
  position: relative;
}
.no-video .video-container video,
.touch .video-container video {
  display: none;
}
.no-video .video-container .poster,
.touch .video-container .poster {
  display: block !important;
}
.video-container {
  position: relative;
  bottom: 0%;
  left: 0%;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: #000;
}
.video-container .poster img {
  width: 100%;
  bottom: 0;
  position: absolute;
}

.video-container .title-container {
  z-index: 1000;
  position: absolute;
  top: 35%;
  width: 100%;
  text-align: center;
  color: #fff;
}
.video-container .description .inner {
  font-size: 1em;
  width: 45%;
  margin: 0 auto;
}
.video-container .link {
  position: absolute;
  bottom: 3em;
  width: 100%;
  text-align: center;
  z-index: 1001;
  font-size: 2em;
  color: #fff;
}
.video-container .link a {
  color: #fff;
}
.video-container video {
  position: absolute;
  z-index: 0;
  bottom: 0;
  
}
.video-container video.fillWidth {
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<section id="intro" class="parallax-section">
<!-- Main jumbotron for a primary marketing message or call to action -->
<div class="homepage-hero-module">
    <div class="video-container">
        <div class="title-container">
            <div class="headline">
                	<h1>Welcome to our Company</h1>

            </div>
            <div class="description">
                <div class="inner">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
            </div>
        </div>
        <div class="filter"></div>
        <video autoplay class="fillWidth">
            <source src="http://video.blendertestbuilds.de/download.blender.org/peach/trailer_480p.mov" type="video/mp4" autoplay loop muted />Your browser does not support the video tag. I suggest you upgrade your browser.
           <source src="http://video.webmfiles.org/big-buck-bunny_trailer.webm" type="video/webm"  autoplay loop muted />Your browser does not support the video tag. I suggest you upgrade your browser.
            </video>
        <div class="poster hidden">
            <img src="images/background.jpg" alt="">
        </div>
    </div>
</div>
<div class="container" id="content">
    <!-- adding this soon -->
    </div>
</section>

【问题讨论】:

  • 先用position:fixed添加视频背景..然后用position:absolute;添加文字
  • 容器类中可能有padding...去掉填充,应该没问题
  • .video-container div 设置为 width:100% 并具有 background-colorblack。在我看来,这正是你所看到的。当您说“但我不希望容器出现在边缘”时,您到底是什么意思?你想让容器更小,这样它就不会覆盖页面的整个宽度吗?或者也许你想让它的背景透明,这样你就能看到它背后的东西?还是别的什么?
  • 是的,抱歉我没有具体说明。我只想要页面全宽的视频,容器就在那里以防万一。通常视频应该覆盖容器。
  • 即使我删除了容器背景,视频仍然没有全宽显示

标签: javascript jquery html css


【解决方案1】:

您可以使用the object-fit property 来控制视频在其元素中的大小。

如果您想拉伸视频以完全填充整个元素(就像img 默认情况下所做的那样),请使用:

object-fit: fill;

如果您想保持现有的纵横比,并且不介意裁剪顶部和底部,请使用:

object-fit: cover;

$( document ).ready(function() {

    // Resive video
    scaleVideoContainer();

    initBannerVideoSize('.video-container .poster img');
    initBannerVideoSize('.video-container .filter');
    initBannerVideoSize('.video-container video');
        
    $(window).on('resize', function() {
        scaleVideoContainer();
        scaleBannerVideoSize('.video-container .poster img');
        scaleBannerVideoSize('.video-container .filter');
        scaleBannerVideoSize('.video-container video');
    });

});

/** Reusable Functions **/
/********************************************************************/

function scaleVideoContainer() {

    var height = $(window).height();
    var unitHeight = parseInt(height) + 'px';
    $('.homepage-hero-module').css('height',unitHeight);

}

function initBannerVideoSize(element){
    
    $(element).each(function(){
        $(this).data('height', $(this).height());
        $(this).data('width', $(this).width());
    });

    scaleBannerVideoSize(element);

}

function scaleBannerVideoSize(element){

    var windowWidth = $(window).width(),
        windowHeight = $(window).height(),
        videoWidth,
        videoHeight;
    
    console.log(windowHeight);

    $(element).each(function(){
        var videoAspectRatio = $(this).data('height')/$(this).data('width'),
            windowAspectRatio = windowHeight/windowWidth;

        if (videoAspectRatio > windowAspectRatio) {
            videoWidth = windowWidth;
            videoHeight = videoWidth * videoAspectRatio;
            $(this).css({'top' : -(videoHeight - windowHeight) / 2 + 'px', 'margin-left' : 0});
        } else {
            videoHeight = windowHeight;
            videoWidth = videoHeight / videoAspectRatio;
            $(this).css({'margin-top' : 0, 'margin-left' : -(videoWidth - windowWidth) / 2 + 'px'});
        }

        $(this).width(videoWidth).height(videoHeight);

        $('.homepage-hero-module .video-container video').addClass('fadeIn animated');
        

    });
}
.homepage-hero-module {
  border-right: none;
  border-left: none;
  position: relative;
}
.no-video .video-container video,
.touch .video-container video {
  display: none;
}
.no-video .video-container .poster,
.touch .video-container .poster {
  display: block !important;
}
.video-container {
  position: relative;
  bottom: 0%;
  left: 0%;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: #000;
}
.video-container .poster img {
  width: 100%;
  bottom: 0;
  position: absolute;
}

.video-container .title-container {
  z-index: 1000;
  position: absolute;
  top: 35%;
  width: 100%;
  text-align: center;
  color: #fff;
}
.video-container .description .inner {
  font-size: 1em;
  width: 45%;
  margin: 0 auto;
}
.video-container .link {
  position: absolute;
  bottom: 3em;
  width: 100%;
  text-align: center;
  z-index: 1001;
  font-size: 2em;
  color: #fff;
}
.video-container .link a {
  color: #fff;
}
.video-container video {
  position: absolute;
  z-index: 0;
  bottom: 0;
  object-fit: cover;
}
.video-container video.fillWidth {
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<section id="intro" class="parallax-section">
<!-- Main jumbotron for a primary marketing message or call to action -->
<div class="homepage-hero-module">
    <div class="video-container">
        <div class="title-container">
            <div class="headline">
                	<h1>Welcome to our Company</h1>

            </div>
            <div class="description">
                <div class="inner">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
            </div>
        </div>
        <div class="filter"></div>
        <video autoplay class="fillWidth">
            <source src="http://video.blendertestbuilds.de/download.blender.org/peach/trailer_480p.mov" type="video/mp4" autoplay loop muted />Your browser does not support the video tag. I suggest you upgrade your browser.
           <source src="http://video.webmfiles.org/big-buck-bunny_trailer.webm" type="video/webm"  autoplay loop muted />Your browser does not support the video tag. I suggest you upgrade your browser.
            </video>
        <div class="poster hidden">
            <img src="images/background.jpg" alt="">
        </div>
    </div>
</div>
<div class="container" id="content">
    <!-- adding this soon -->
    </div>
</section>

【讨论】:

  • 非常感谢,它成功了!我试图解决这个问题已经很久了,谢谢先生。
  • @Bart 向我指出 object-fit 在 Internet Explorer/Edge 中不起作用。它看起来像Microsoft has decided to implement it eventually,但我想这不是什么安慰。需要有适合这些用户的不同解决方案。
  • 好的,我知道了如何修复它,实际上很容易。只需将容器颜色更改为与背景相同的颜色,这样容器就不会覆盖背景。
猜你喜欢
  • 2017-03-22
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
  • 2011-01-06
  • 2017-05-04
  • 1970-01-01
相关资源
最近更新 更多