I am attempting to display a video in a responsive design such that the scaling borders blend into the background.
我试图在响应式设计中显示视频,以便缩放边框融入背景。
I allow the dimensions of the video element to vary within specified bounds. As a result, when the video playing doesn't fill the actual html element, I get the black padding bars around my video.
我允许视频元素的尺寸在指定的范围内变化。因此,当视频播放没有填充实际的html元素时,我的视频周围会出现黑色填充条。
Using the css background property I have been able to change the color of the bars shown in Chrome, FireFox, and Opera. I cannot figure out how to change the color shown for Internet Explorer or iOS (ipad).
使用css background属性,我可以更改Chrome,FireFox和Opera中显示的条形颜色。我无法弄清楚如何更改Internet Explorer或iOS(ipad)显示的颜色。
Can anyone help me out with this?
任何人都可以帮我解决这个问题吗?
fiddle as requested: http://jsfiddle.net/swaEe/
根据要求提供小提琴:http://jsfiddle.net/swaEe/
html:
css:
video {
width: 500px;
background: blue;
}
***_ edit _***
*** _编辑_ ***
This is a better fiddle: http://jsfiddle.net/swaEe/40/
这是一个更好的小提琴:http://jsfiddle.net/swaEe/40/
The video playback should stay vertically and horizontally centered in the container. I want the "bars" to be transparent or the same color as the container (red in this case...).
视频播放应在容器中垂直和水平居中。我希望“条形”是透明的或与容器颜色相同(在这种情况下为红色......)。
2
How about div with css as background? [I'm not familiar with iOS, tasted on IE11]
用css作为背景的div怎么样? [我不熟悉iOS,在IE11上品尝过]
html:
CSS:
video {
display: block;
margin-left: auto;
margin-right: auto;
}
#container{
width: 500px;
height: 240px;
background: blue;
}
jsfiddle: http://jsfiddle.net/c9aHf/1/
2
Solved! Link to the live jsbin: http://jsbin.com/AVOZoXu/9
The edit jsbin to follow the explanation: http://jsbin.com/AVOZoXu/9/edit
I couldn't test it on IE but it should work like a charm.
解决了!链接到现场jsbin:http://jsbin.com/AVOZoXu/9编辑jsbin按照说明:http://jsbin.com/AVOZoXu/9/edit我无法在IE上测试但它应该工作喜欢魅力。
There is a real problem with iOS. You won't be able to set a background color to the player and the default player size is 150x300 as you can see in Safari Developer Library:
iOS存在一个真正的问题。您将无法为播放器设置背景颜色,默认播放器大小为150x300,如Safari Developer Library中所示:
Because the native dimensions of a video are not known until the movie metadata loads, a default height and width of 150 x 300 is allocated on devices running iOS if the height or width is not specified. Currently, the default height and width do not change when the movie loads, [...]
由于在加载电影元数据之前不知道视频的原生尺寸,因此如果未指定高度或宽度,则在运行iOS的设备上分配默认高度和宽度150 x 300。目前,电影加载时默认的高度和宽度不会改变,[...]
So, what you have to do to remove the black bars is do change the default size and adapt it to the movie size as soon as you can. And yes, we'll need Javascript.
因此,要删除黑条,您需要做的是更改默认大小并尽快使其适应电影大小。是的,我们需要Javascript。
// set height and width to native values
function naturalSize() {
var myVideo = document.getElementById('theVideo');
var myCOntent= document.getElementById('content');
myVideo.height = myVideo.videoHeight;
myVideo.width = myVideo.videoWidth;
//if the video is bigger than the container it'll fit
ratio = myVideo.videoWidth/myVideo.videoHeight;
if(parseInt(myContent.offsetWidth,10)
And now you'll get a video player size of the video as soon as the meta data loads. Since the video doesn't have its black bars anymore, I just had to center it as text.
现在,只要元数据加载,您就会获得视频的视频播放器大小。由于视频不再有黑条,我只需将其作为文本居中。
Oh! And you wanted it to be responsive? Check it out, it doesn't matter the width you set to the #content
because naturalSize()
checks the ratio and the container's width and sets a smaller height for the video than the original, preventing the black bars appearing in original video height with a smaller width.
哦!而你希望它具有响应能力?检查一下,你设置为#content的宽度并不重要,因为naturalSize()会检查比率和容器的宽度,并为视频设置一个比原始高度更小的高度,防止黑条出现在原始视频高度中宽度更小。
The width is controlled with the max-width:100%;
property so there's no need to change it manually.
宽度由最大宽度控制:100%;属性所以不需要手动更改它。
#content{
background:blue;
width:50%;
height:auto;
text-align:center;
}
video {
max-width:100%;
vertical-align:top;
}
I know, I know, the video doesn't get resized till you have started playing it, but it's the closest you're gonna get on iOS to do what you want. Anyway, I think it's a great solution, I hope it helps you.
我知道,我知道,视频在你开始播放之前不会调整大小,但它是最接近iOS的,你可以做你想做的事情。无论如何,我认为这是一个很好的解决方案,我希望它对你有所帮助。
2
I think I've managed to come up with a solution:
我想我已经设法提出了一个解决方案:
The problem is that it seems to be impossible to style these letterboxes cross-browser. So the trick then would be not to have letterboxes, by scaling the video element to the aspect ratio of the video file.
问题是,似乎不可能跨浏览器设置这些信箱的样式。因此,通过将视频元素缩放到视频文件的宽高比,诀窍就是没有信箱。
This solution has two potential drawbacks:
该解决方案有两个潜在的缺点:
它需要Javascript
you need to know the dimensions of the video file and write them into data
-attributes
您需要知道视频文件的尺寸并将其写入数据属性
The reason for this is that the browser does not know the dimensions of the video file until it has started loading it. Most browsers do load a few bytes of the video before it is played, but not all - some older versions of Android wait until the user starts playing the video.
原因是浏览器在开始加载视频文件之前不知道视频文件的尺寸。大多数浏览器在播放前会加载几个字节的视频,但不是全部 - 一些旧版本的Android会等到用户开始播放视频。
If you do not care about Android 2.3, waiting for the loadedmetadata
event to get videoWidth and videoHeight as jaicabs answer does it is the right way.
如果你不关心Android 2.3,那么等待loadedmetadata事件将videoWidth和videoHeight作为jaicabs的答案,这是正确的方法。
Take a look at this: run fiddle / fiddle editor
看看这个:运行小提琴/小提琴编辑器
We basically do three things:
我们基本上做了三件事:
计算视频的宽高比
调整它的大小,使其紧贴其容器
在容器内水平和垂直居中
You can now simply set the background-color of the container, and you're done.
您现在可以简单地设置容器的背景颜色,然后就完成了。
I've tested it with iOS 7 on iPhone and iPad, Chrome, Firefox and Safari. No IE testing so far, since I currently don't have my virtual machines handy, but I foresee no problems here for the current IEs.
我已经在iPhone和iPad,Chrome,Firefox和Safari上使用iOS 7进行了测试。到目前为止还没有IE测试,因为我目前没有方便的虚拟机,但我预见到当前的IE没有问题。
0
删除内联宽度/高度属性。您想使用CSS来控制您的布局!
为你的身高使用魔术关键字'auto'
确保还使用与视频宽高比相同的海报
Here is a fiddle: http://jsfiddle.net/swaEe/13/
这是一个小提琴:http://jsfiddle.net/swaEe/13/
video {
width: 500px;
min-width: 200px;
max-width: 100%;
height: auto;
}
0
Solved with no JS:
解决没有JS:
Basically, in both cases removing the height or width setting for the video (for the thin div, I removed the height, and for the short one, the width). Then centered the video elements (horizontal with display:block and then the margin trick, vertical with display:table-cell, and there's probably a better way to do that one).
基本上,在两种情况下都删除了视频的高度或宽度设置(对于瘦div,我删除了高度,对于短的高度,宽度)。然后将视频元素居中(水平显示:块然后是边缘技巧,垂直显示:table-cell,这可能是更好的方法)。
0
I know some time has already passed since the question was asked, but I also had to implement a workaround for this problem and would like to share. The problem was similar to OP's, in that the video could be any size or aspect ratio.
我知道自提出问题以来已经过了一段时间,但我还必须为这个问题实施一个解决方法,并希望分享。问题类似于OP,因为视频可以是任何大小或宽高比。
If the HTML 如果HTML
Knowing this, we can take advantage of the 知道了这一点,我们可以利用loadedmetadata事件:我们实际上并不需要视频的维度来进行任何计算,但我们必须等待加载这些数据,以便容器调整大小。一旦发生这种情况,我们就可以水平和/或垂直调整容器的位置。 Here's a fiddle tested in IE11: 这是在IE11中测试的小提琴: http://jsfiddle.net/j6Lnz31y/ Source (in case the fiddle ever becomes unavailable): 来源(如果小提琴变得不可用): element is contained within a
loadedmetadata
event: we don't actually need the video's dimensions for any calculations, but we must wait for this data to load so that the container will resize. As soon as that happens, we can adjust the container's position horizontally and/or vertically.
.whatever-container {
position: relative;
}
.video-container {
position: absolute;
opacity: 0;
}
.video-container > video {
position: relative;
width: 100%;
height: 100%;
}
/*
* After the video dimentions have been acquired, its container will have
* resized and we can adjust its position as necessary.
*/
function adjustVideoContainer() {
console.log("video metadata loaded");
var videoCOntainer= $(this).parent().filter(".video-container");
if (videoContainer.length === 0) {
//abort
console.log(
"adjustVideoContainer() was called but no it wasn't "+
"wrapped in an appropriate container"
);
return;
}
var cOntainerParent= videoContainer.parent();
var parentWidth = containerParent.width(),
parentHeight = containerParent.height(),
cOntainerWidth= videoContainer.width(),
cOntainerHeight= videoContainer.height();
if (containerWidth