A-A+

js获取视频video标签的宽(width)高(height)

2020年03月04日 JavaScript 暂无评论 阅读 1,551 次

js获取视频video标签的宽(width)高(height)

     getVideoHeight (url) {
      return new Promise((resolve, reject) => {
        var video = document.createElement('video')
        video.autoplay = true
        video.oncanplay = function () {
          resolve({width: this.offsetWidth, height: this.offsetHeight})
          this.src = 'about:blank'
          document.body.removeChild(video)
        }

        document.body.appendChild(video)
        video.src = url
      })
    },
    // 根据140这个阀值,来计算一个最大高度
      let videoHeight = 0
      let videoWidth = 0
      if (width > height && width > 140) {
        videoHeight = height / (width / 140)
        videoWidth = 140
      } else if ((width <= height && height > 140) || (width <= 140 && height <= 140)) {
        videoHeight = 140
        videoWidth = width
      } else {
        videoHeight = height
        videoWidth = width
      }
标签:

给我留言

Copyright © web前端技术开发个人博客 保留所有权利  京ICP备14060653号 Theme  Ality

用户登录