Html 如何获取 html5 视频元素的宽度和高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10512529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:18:01  来源:igfitidea点击:

How can I get the html5 video element's width and height

htmlvideoheightwidth

提问by hh54188

I have a simple html5 video tag:

我有一个简单的 html5 视频标签:

 <video autobuffer="true" id="video" controls="true">
        <source src="some_url"></scource>
  </video>

I have not define a obvious width and height in video tag or in css,the video wrapper would adjust according to the source video size,the problem is, how can I get the video current width and height? I have tried jquery

我没有在视频标签或css中定义明显的宽度和高度,视频包装器会根据源视频大小进行调整,问题是,如何获得视频当前的宽度和高度?我试过 jquery

$('video').css('width');

but it return me the origin size :300px

但它返回原点大小:300px

what I see in the page is 800px!

我在页面中看到的是 800px!

How can I solve this problem?

我怎么解决这个问题?

回答by Neil

$(function () {

    $("#video").bind("loadedmetadata", function () {
        var width = this.videoWidth;
        var height = this.videoHeight;
        // ...

    });

});

回答by Shantanu Wagh

Just found out that we can get the video width and height as:

刚刚发现我们可以得到视频的宽度和高度为:

$("#video").innerHeight();
$("#video").innerWidth();

These two functions are exposed by jQuery.

这两个函数由 jQuery 公开。

Another reason why jQuery rocks!!!

jQuery 摇滚的另一个原因!!!