【问题标题】:What are the width and height properties of a video element?视频元素的宽度和高度属性是什么?
【发布时间】:2021-01-07 21:46:58
【问题描述】:

取这个<video>元素:

<video height=5 width=10><source src="..." type="video/mp4"></video>

设置height=5 width=10,或随后通过JavaScript设置这些属性有什么效果?

通过实验,我知道的唯一效果是 widthheight 属性用作以 CSS 像素为单位的元素大小(如果没有通过 CSS 指定大小)。

这样,&lt;video&gt; 元素的行为类似于&lt;canvas&gt; 元素。但是在&lt;canvas&gt; 元素上,widthheight 属性还有另一个用途:设置支持画布的二维像素数组的尺寸。然后将这个二维像素数组缩放到画布元素的显示区域(默认使用object-fit: fillimage-rendering: pixelated)。

widthheight 属性似乎没有为视频设置任何类型的“支持数组大小”。在&lt;video&gt; 元素上设置widthheight 属性时,它不会对视频流的像素化产生明显的影响。

视频中的像素数似乎只是来自媒体流本身——如&lt;video&gt; 元素的videoWidthvideoHeight 属性或metadata.widthmetadata.height 中所示赋予requestVideoFrameCallback 回调的帧的属性。

除非明确设置,否则 widthheight 属性的值是 0,正如 JavaScript 所报告的那样。即使在播放视频时也是如此。相比之下,widthheight 属性 &lt;canvas&gt; 默认为 300150,这是支持它的二维像素数组的真实大小。

The MDN documentation 将这些属性描述为

HTMLVideoElement.height

是一个DOMString,反映了html的height属性,它指定了显示区域的高度,以CSS像素为单位。

HTMLVideoElement.width

是一个DOMString,反映了html的width属性,它指定了显示区域的宽度,以CSS像素为单位。

但这似乎不正确,因为默认值 0 是为显然没有零显示区域的视频提供的。我也不相信这个描述,因为“显示区域的宽度/高度,以 CSS 像素为单位”可以通过 CSS 完美设置,使得这些属性完全多余。

那么,&lt;video&gt; 元素的widthheight 属性是什么?除了作为设置 CSS 大小的冗余方式之外,它们还有什么用途?

【问题讨论】:

  • 你是如何在 中达到零的"不过,这似乎不正确,因为默认值 0 是为显然没有零显示区域的视频提供的。'
  • 通过在JavaScript中查询播放视频的.width属性
  • 你能贴出你使用的代码吗?

标签: html css canvas video


【解决方案1】:

它们与&lt;iframe&gt;&lt;object&gt; 的相同,与&lt;img&gt; 的相似:

它们用作设置显示大小的回退,对于内联替换元素,按此顺序计算(如果在回退之前找到,则保留任何找到的宽度或高度):

  1. CSS 定义的宽度 + 高度
  2. 属性定义宽度 + 高度
  3. 纵横比 x 先前选择的宽度或高度
  4. 媒体定义的宽度 + 高度
  5. 300 x 150 像素

(对于 SVG 图像等媒体,步骤 3 和 4 实际上更复杂)。

document.querySelectorAll("video").forEach( elem =&gt; elem.src = "https://upload.wikimedia.org/wikipedia/commons/a/a4/BBH_gravitational_lensing_of_gw150914.webm");
.css-width {
  width: 200px;
}
.css-height {
  height: 200px;
}
video {
  border: 1px solid;
}
section {
  border-bottom: 1px solid;
}
<section>
  <p>CSS width + CSS height + width attribute + height attribute</p>
  <video class="css-width css-height" width="10" height="10"></video>
  <p>CSS dimensions win</p>
</section>
<section>
  <p>CSS width + width attribute + height attribute:</p>
  <video class="css-width" width="10" height="10"></video>
  <p>CSS dimensions is used for width, attribute is used for height</p>
</section>
<section>
  <p>CSS width + height attribute:</p>
  <video class="css-width" height="10"></video>
  <p>Attributes are used</p>
</section>
<section>
  <p>CSS width only:</p>
  <video class="css-width"></video>
  <p>CSS width is used for width, height is set to the intrinsic aspect-ratio * width</p>
</section>
<section>
  <p>width attribute only:</p>
  <video width="10"></video>
  <p>Attribute width is used for width, height is set to the intrinsic aspect-ratio * width</p>
</section>
<section>
  nothing:<br>
  <video></video>
  <p>intrinsic dimensions are used</p>
</section>

然而,它们与&lt;img&gt; 的不同之处在于at getting &lt;img&gt; ones 将返回呈现的CSS 尺寸,而对于&lt;video&gt;(和其他),它只会返回属性的值,强制为其类型(@ 987654332@ 为&lt;video&gt;)。

因此,如果您没有将任何内容设置为属性,那么无论呈现的尺寸如何,它都会返回 0

【讨论】:

  • 感谢@Kaiido,这很有帮助。这似乎证实了我的感觉,这些属性实际上并没有有用,因为 CSS 可以执行相同的工作。我的猜测是它们仅出于历史原因而存在,应该避免设置或访问它们。
  • 在 CSS 规则生效之前设置它们并没有什么坏处。
  • 嗯,根据您与img 的比较,我发现this argument in favor of using these attributes - 在加载之前告诉UA 媒体的固有尺寸,创建一个占位符。然后可以使用 CSS 来缩放图像,使用由widthheight 属性定义的纵横比。我半信半疑。再次感谢您的帮助!
  • 这对于计算初始纵横比绝对有用,现代浏览器不会根据这些值进行计算。
猜你喜欢
  • 1970-01-01
  • 2013-12-06
  • 2014-11-23
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 2019-07-02
  • 1970-01-01
  • 2019-05-03
相关资源
最近更新 更多