Javascript - 如何在不按高度切割图片的情况下将视频屏幕固定为拉伸(stretch)宽度和高度

标签 javascript jquery css html google-chrome

这是不正确的,我正在尝试将视频屏幕完全设置为 100% 宽度和 100% 高度。但是在更大的分辨率下它不能正确显示高度(离开 View )。

The following aspect ration is working but the problem is it makes wrong (black on left and right):

enter image description here

window.onresize = function(){
  var aspectRatio;
  if (remoteVideo.style.opacity === '1') {
    aspectRatio = remoteVideo.videoWidth/remoteVideo.videoHeight;
  } else if (localVideo.style.opacity === '1') {
    aspectRatio = localVideo.videoWidth/localVideo.videoHeight;
  } else {
    return;
  }

  var innerHeight = this.innerHeight;
  var innerWidth = this.innerWidth;
  var videoWidth = innerWidth < aspectRatio * window.innerHeight ?
                   innerWidth : aspectRatio * window.innerHeight;
  var videoHeight = innerHeight < window.innerWidth / aspectRatio ?
                    innerHeight : window.innerWidth / aspectRatio;
  containerDiv = document.getElementById('container');
  containerDiv.style.width = videoWidth + 'px';
  containerDiv.style.height = videoHeight + 'px';
  containerDiv.style.left = (innerWidth - videoWidth) / 2 + 'px';
  containerDiv.style.top = (innerHeight - videoHeight) / 2 + 'px';
};

Tried to modify this but still when the width increase then the height get cut off also while using webkitRequestFullScreen(); the whole width and height is broken, its not doing accurate.

window.onresize = function(){
  var aspectRatio;
  if (remoteVideo.style.opacity === '1') {
    aspectRatio = remoteVideo.videoWidth/remoteVideo.videoHeight;
  } else if (localVideo.style.opacity === '1') {
    aspectRatio = localVideo.videoWidth/localVideo.videoHeight;
  } else {
    return;
  }

  var innerHeight = this.innerHeight;
  var innerWidth = this.innerWidth;
  var videoWidth = innerWidth < aspectRatio * window.innerHeight ?
                   innerWidth : aspectRatio * window.innerHeight;
  var videoHeight = innerHeight < window.innerWidth / aspectRatio ?
                    innerHeight : window.innerWidth / aspectRatio;
  containerDiv = document.getElementById('container');
  //containerDiv.style.width = videoWidth + 'px';
  containerDiv.style.width = window.innerWidth + 'px';
  containerDiv.style.height = videoHeight + 'px';
  //containerDiv.style.left = (innerWidth - videoWidth) / 2 + 'px';
  //containerDiv.style.top = (innerHeight - videoHeight) / 2 + 'px';
};

预期输出(调整大小、最大化或全屏):

enter image description here

任何人都可以提出建议,我完全迷路了,无法做出准确的预期输出。

编辑: http://jsfiddle.net/2m8ay/

最佳答案

你可以为此尝试 css:

video { 
       width:305px; 
       height:160px;  
       background:blue; 
    }

如果视频海报包含图像,则

video { 
   width:305px; 
   height:160px;  
   background:transparent url('poster.jpg') no-repeat 0 0; 
   -webkit-background-size:cover; 
   -moz-background-size:cover; 
   -o-background-size:cover; 
   background-size:cover; 
}

但是,这仅在您的视频数量非常固定时才有用。否则,您将需要执行内联 css 以在那里重置背景图像。

更新您的解决方案:http://jsfiddle.net/2m8ay/1/

关于Javascript - 如何在不按高度切割图片的情况下将视频屏幕固定为拉伸(stretch)宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21425377/

相关文章:

html - CSS3加载循环

html - IMG 和 H2 元素之间的边距崩溃不起作用

javascript - Bootstrap slider 不工作

javascript - 如何使用angularjs在visjs中添加放大缩小按钮?

javascript - 在 Firefox 和 Chrome 中将 <br/> 更改为\n\r

javascript - jQuery 中的链接选择器

jquery - 销毁 jquery 函数和我的对象之间的绑定(bind)?

javascript - 如何将检查值链接到数组中的特定对象

javascript - 查找并删除数组元素

javascript - [].slice 或 Array.prototype.slice