Video 100% width and height

后端 未结 13 2024
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 06:57

    You can use Javascript to dynamically set the height to 100% of the window and then center it using a negative left margin based on the ratio of video width to window width.

    http://jsfiddle.net/RfV5C/

    var $video  = $('video'),
        $window = $(window); 
    
    $(window).resize(function(){
        var height = $window.height();
        $video.css('height', height);
    
        var videoWidth = $video.width(),
            windowWidth = $window.width(),
        marginLeftAdjust =   (windowWidth - videoWidth) / 2;
    
        $video.css({
            'height': height, 
            'marginLeft' : marginLeftAdjust
        });
    }).resize();
    

提交回复
热议问题