首页 > 解决方案 > 如何获取视频海报的图像尺寸?

问题描述

如何获取元素中poster图像的图像尺寸(宽度和高度)?<video poster="…">jQuery 是可以接受的。

标签: javascriptjqueryhtmlimagehtml5-video

解决方案


如果您的<video>元素被引用为yourVideo(例如const yourVideo = document.querySelector("video");),那么您可以通过该属性获取其posterURL 。poster这只是一个图像 URL,因此可以像使用任何图像 URL 一样获取其尺寸:

const poster = Object.assign(new Image(), {
    src: yourVideo.poster
  });

poster.addEventListener("load", () => console.log(poster.width, poster.height));

推荐阅读