Video 100% width and height

后端 未结 13 1993
Happy的楠姐
Happy的楠姐 2020-12-08 06:37

I have a video, and I want it to FILL 100% of the width, and 100% of the height. And keep the aspect ratio.

Is it possible that it at least fills 100% for both? And

相关标签:
13条回答
  • 2020-12-08 07:22

    I use JavaScript and CSS to accomplish this. The JS function needs to be called once on init and on window resize. Just tested in Chrome.

    HTML:

    <video width="1920" height="1080" controls>
        <source src="./assets/video.mp4" type="video/mp4">
        Your browser does not support the video tag.
    </video>
    

    JavaScript:

    function scaleVideo() {
        var vid = document.getElementsByTagName('video')[0];
        var w = window.innerWidth;
        var h = window.innerHeight;
    
        if (w/16 >= h/9) {
            vid.setAttribute('width', w);
            vid.setAttribute('height', 'auto');
        } else {
            vid.setAttribute('width', 'auto');
            vid.setAttribute('height', h);
        }
    }
    

    CSS:

    video {
        position:absolute;
        left:50%;
        top:50%;
        -webkit-transform:translate(-50%,-50%);
        transform:translate(-50%,-50%);
    }
    
    0 讨论(0)
提交回复
热议问题