热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

无法在IE和iOS中更改html5视频黑条的颜色-Cannotchangecolorofhtml5videoblackbarsinIEandiOS

Iamattemptingtodisplayavideoinaresponsivedesignsuchthatthescalingbordersblendintot

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...).

视频播放应在容器中垂直和水平居中。我希望“条形”是透明的或与容器颜色相同(在这种情况下为红色......)。


6 个解决方案

#1


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


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的,你可以做你想做的事情。无论如何,我认为这是一个很好的解决方案,我希望它对你有所帮助。

#3


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:

该解决方案有两个潜在的缺点:

  1. it requires Javascript
  2. 它需要Javascript

  3. 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:

我们基本上做了三件事:

  1. calculate the aspect ratio of the video
  2. 计算视频的宽高比

  3. resize it so that it fits snugly into its container
  4. 调整它的大小,使其紧贴其容器

  5. center it horizontally and vertically within the container
  6. 在容器内水平和垂直居中

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没有问题。

#4


0  

  1. remove the inline width/height attributes. You want to use CSS to control your layout!
  2. 删除内联宽度/高度属性。您想使用CSS来控制您的布局!

  3. Use the magic keyword 'auto' for your height
  4. 为你的身高使用魔术关键字'auto'

  5. Make sure to also use a poster with the same aspect ratio of your video
  6. 确保还使用与视频宽高比相同的海报

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;
}

#5


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,这可能是更好的方法)。

#6


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 element is contained within a

which specifies no size at all, the container will automatically fit itself around the video and it will have no black "padding".

如果HTML

中,它根本没有指定大小,则容器将自动适应视频,并且没有黑色的“填充”。

Knowing this, we can take advantage of the 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.

知道了这一点,我们可以利用loadedmetadata事件:我们实际上并不需要视频的维度来进行任何计算,但我们必须等待加载这些数据,以便容器调整大小。一旦发生这种情况,我们就可以水平和/或垂直调整容器的位置。

Here's a fiddle tested in IE11:

这是在IE11中测试的小提琴:

http://jsfiddle.net/j6Lnz31y/

Source (in case the fiddle ever becomes unavailable):

来源(如果小提琴变得不可用):



.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 

推荐阅读
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • Docker的安全基准
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • PyCharm下载与安装指南
    本文详细介绍如何从官方渠道下载并安装PyCharm集成开发环境(IDE),涵盖Windows、macOS和Linux系统,同时提供详细的安装步骤及配置建议。 ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文详细介绍了如何解决Uploadify插件在Internet Explorer(IE)9和10版本中遇到的点击失效及JQuery运行时错误问题。通过修改相关JavaScript代码,确保上传功能在不同浏览器环境中的一致性和稳定性。 ... [详细]
  • 本文介绍了如何利用JavaScript或jQuery来判断网页中的文本框是否处于焦点状态,以及如何检测鼠标是否悬停在指定的HTML元素上。 ... [详细]
  • 探讨如何高效使用FastJSON进行JSON数据解析,特别是从复杂嵌套结构中提取特定字段值的方法。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
author-avatar
丿氵小柒柒2502894463
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有