热门标签 | 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 

推荐阅读
  • 本文详细解析了 Yii2 框架中视图和布局的各种函数,并综述了它们在实际开发中的应用场景。通过深入探讨每个函数的功能和用法,为开发者提供了全面的参考,帮助他们在项目中更高效地利用这些工具。 ... [详细]
  • javascript分页类支持页码格式
    前端时间因为项目需要,要对一个产品下所有的附属图片进行分页显示,没考虑ajax一张张请求,所以干脆一次性全部把图片out,然 ... [详细]
  • 本文深入探讨了 hCalendar 微格式在事件与时间、地点相关活动标记中的应用。作为微格式系列文章的第四篇,前文已分别介绍了 rel 属性用于定义链接关系、XFN 微格式增强链接的人际关系描述以及 hCard 微格式对个人和组织信息的描述。本次将重点解析 hCalendar 如何通过结构化数据标记,提高事件信息的可读性和互操作性。 ... [详细]
  • 网站访问全流程解析
    本文详细介绍了从用户在浏览器中输入一个域名(如www.yy.com)到页面完全展示的整个过程,包括DNS解析、TCP连接、请求响应等多个步骤。 ... [详细]
  • 在分析和解决 Keepalived VIP 漂移故障的过程中,我们发现主备节点配置如下:主节点 IP 为 172.16.30.31,备份节点 IP 为 172.16.30.32,虚拟 IP 为 172.16.30.10。故障表现为监控系统显示 Keepalived 主节点状态异常,导致 VIP 漂移到备份节点。通过详细检查配置文件和日志,我们发现主节点上的 Keepalived 进程未能正常运行,最终通过优化配置和重启服务解决了该问题。此外,我们还增加了健康检查机制,以提高系统的稳定性和可靠性。 ... [详细]
  • Web开发框架概览:Java与JavaScript技术及框架综述
    Web开发涉及服务器端和客户端的协同工作。在服务器端,Java是一种优秀的编程语言,适用于构建各种功能模块,如通过Servlet实现特定服务。客户端则主要依赖HTML进行内容展示,同时借助JavaScript增强交互性和动态效果。此外,现代Web开发还广泛使用各种框架和库,如Spring Boot、React和Vue.js,以提高开发效率和应用性能。 ... [详细]
  • HTML5 Web存储技术是许多开发者青睐本地应用程序的重要原因之一,因为它能够实现在客户端本地存储数据。HTML5通过引入Web Storage API,使得Web应用程序能够在浏览器中高效地存储数据,从而提升了应用的性能和用户体验。相较于传统的Cookie机制,Web Storage不仅提供了更大的存储容量,还简化了数据管理和访问的方式。本文将从基础概念、关键技术到实际应用,全面解析HTML5 Web存储技术,帮助读者深入了解其工作原理和应用场景。 ... [详细]
  • IOS Run loop详解
    为什么80%的码农都做不了架构师?转自http:blog.csdn.netztp800201articledetails9240913感谢作者分享Objecti ... [详细]
  • 深入解析HTML5字符集属性:charset与defaultCharset
    本文将详细介绍HTML5中新增的字符集属性charset和defaultCharset,帮助开发者更好地理解和应用这些属性,以确保网页在不同环境下的正确显示。 ... [详细]
  • Unity与MySQL连接过程中出现的新挑战及解决方案探析 ... [详细]
  • 在配置Nginx的SSL证书后,虽然HTTPS访问能够正常工作,但HTTP请求却会遇到400错误。本文详细解析了这一问题,并提供了Nginx配置的具体示例。此外,还深入探讨了DNS服务器证书、SSL证书的申请与安装流程,以及域名注册、查询方法和CDN加速技术的应用,帮助读者全面了解相关技术细节。 ... [详细]
  • CSS中的pointer-events属性详解与应用
    在CSS中,`pointer-events`属性是一个非常实用但常被忽视的功能。它主要用于控制元素是否响应鼠标事件。当一个元素覆盖在其他元素之上时,通过设置`pointer-events`属性,可以决定该元素是否能够接收鼠标点击、悬停等交互操作,从而实现更灵活的用户界面设计。例如,将`pointer-events`设置为`none`可以使元素透明地传递鼠标事件,方便实现复杂的叠加效果和交互逻辑。 ... [详细]
  • 本文详细解析了如何利用Appium与Python在真实设备上执行测试示例的方法。首先,需要开启手机的USB调试功能;其次,通过数据线将手机连接至计算机并授权USB调试权限。最后,在命令行工具中验证设备连接状态,确保一切准备就绪,以便顺利进行测试。 ... [详细]
  • 本文介绍了如何利用摄像头捕捉图像,并将捕获的图像数据保存为文件。通过详细的代码示例,展示了摄像头调用的具体实现方法,适用于多种应用场景,如安全监控、图像处理等。 ... [详细]
  • 探讨.NET技术与Silverlight中控件拖放及复制功能的实现方法
    Silverlight拖动复制控件,就是将控件从一个容器中向另一个容器中拖动时,不是移动控件而把该控件到另一个容器中。这种情形在程序中经常遇到ÿ ... [详细]
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社区 版权所有