最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - Get width and height of HTML5 Video using native JavaScript - Stack Overflow

programmeradmin2浏览0评论

I am trying to get width and height of tag, I am using the following markup:

<div class="masked" id="video_container">
    <video id="video" loop="loop" muted="muted" tabindex="0" style="width: 100%; height: 100%;">
        <source src="..." type="..."</source>
        <source src="..." type="..."</source>
    </video>
</div>

The key part is that width and height attributes are both set to 100%, and when window is changed aspect ratio of video is saved, but I cannot get it's actual width and height.

I tried to get value using offsetWidth and offsetHeight properties, but they are return actual size of the video.

Edit:

This is the piece of code that working for me:

var videoActualWidth = video.getBoundingClientRect().width;
var videoActualHeight = video.getBoundingClientRect().height;

var aspect = videoActualWidth / videoActualHeight;

if (w / h > aspect) {
    video.setAttribute("style", "height: 100%");
} else {
    video.setAttribute("style", "width: 100%");
}

Thanks!

I am trying to get width and height of tag, I am using the following markup:

<div class="masked" id="video_container">
    <video id="video" loop="loop" muted="muted" tabindex="0" style="width: 100%; height: 100%;">
        <source src="..." type="..."</source>
        <source src="..." type="..."</source>
    </video>
</div>

The key part is that width and height attributes are both set to 100%, and when window is changed aspect ratio of video is saved, but I cannot get it's actual width and height.

I tried to get value using offsetWidth and offsetHeight properties, but they are return actual size of the video.

Edit:

This is the piece of code that working for me:

var videoActualWidth = video.getBoundingClientRect().width;
var videoActualHeight = video.getBoundingClientRect().height;

var aspect = videoActualWidth / videoActualHeight;

if (w / h > aspect) {
    video.setAttribute("style", "height: 100%");
} else {
    video.setAttribute("style", "width: 100%");
}

Thanks!

Share Improve this question edited May 9, 2013 at 12:02 Kristian Vitozev asked Apr 29, 2013 at 7:02 Kristian VitozevKristian Vitozev 5,9716 gold badges37 silver badges56 bronze badges 2
  • Do you mean that, once the page has loaded and the width and height established (no matter what the browser dimensions) you want the video to keep those original dimensions, even if the browser width and/or height change? – ralph.m Commented Apr 29, 2013 at 8:06
  • Hello, I have one video and both width and height properties are set to 100% via CSS. My parent container is full-width. My video is responsive this way, when I resize browser window it keeps it's aspect ratio and show white stripes on sides. But in DOM my video element get width of a whole available space, although visible it's smaller. Sorry for my bad explanation, later today I will setup new jsfiddle. – Kristian Vitozev Commented Apr 29, 2013 at 9:21
Add a comment  | 

1 Answer 1

Reset to default 19

This code will give you the dimensions of the video tag (not the dimensions of the video itself)

var video = document.getElementById("video");
var videotag_width = video.offsetWidth;
var videotag_height = video.offsetHeight;

This code will give you the dimensions of the video that is currently playing

var video = document.getElementById("video");
var video_height = video.videoHeight;
var video_width = video.videoWidth;
发布评论

评论列表(0)

  1. 暂无评论