简体   繁体   English

如何使用html5元素调整视频大小?

[英]How to make video resize by using html5 element?

I am trying to show a video on fullscreen. 我正在尝试全屏显示视频。 I am using html5 element and code is below . 我正在使用html5元素,下面是代码。

javascript code JavaScript代码

$('document').ready(function()
       {

             var o_vid_width=$('#vid1').width();
              var o_vid_height=$('#vid1').height();
              alert(o_vid_width);// output 300px

           var height = $(window).height();
           var width = $(window).width();
            alert(width)// //output 1280

             if(o_vid_width<=width){

                o_vid_width=width ;
                  alert(o_vid_height)// out put 150 px
             }
});

HTML Code HTML代码

<video id="vid1" autoplay loop >

  <source src="viedos/viedo.mp4" type="video/mp4" >

  <source src="viedos/viedo.ogg" type="video/ogg" >
  Your browser does not support the video tag.
</video>

In above code I am trying to reassign the value of my window width to o_vid_width ,it should be 1280px; 在上面的代码中,我试图将窗口宽度的值重新分配给o_vid_width,它应该为1280px; but it giving me 150 . 但这给了我150。 I dont know why it behaving strange? 我不知道为什么它表现得奇怪?

// I tried below code also even it showing margins on both side .I want that it would cover whole window and when I decrease the size even that it will be shown at whole windows no margins. //我尝试了下面的代码,即使它在两侧都显示了边距。我希望它可以覆盖整个窗口,并且当我减小尺寸时,即使它在整个窗口上都显示没有边距。 var min_w = 300; var min_w = 300; // minimum video width allowed var vid_w_orig; //允许的最小视频宽度var vid_w_orig; // original video dimensions var vid_h_orig; //原始视频尺寸var vid_h_orig;

jQuery(function() { // runs after DOM has loaded jQuery(function(){//在DOM加载后运行

vid_w_orig = parseInt(jQuery('video').attr('width'));
vid_h_orig = parseInt(jQuery('video').attr('height'));


jQuery(window).resize(function () { resizeToCover(); });
jQuery(window).trigger('resize');

}); });

function resizeToCover() { 函数resizeToCover(){

// set the video viewport to the window size
jQuery('#vid1').width(jQuery(window).width());
jQuery('#vid1').height(jQuery(window).height());

// use largest scale factor of horizontal/vertical
var scale_h = jQuery(window).width() / vid_w_orig;
var scale_v = jQuery(window).height() / vid_h_orig;
var scale = scale_h > scale_v ? scale_h : scale_v;

// don't allow scaled width < minimum video width
if (scale * vid_w_orig < min_w) {scale = min_w / vid_w_orig;};

// now scale the video
jQuery('video').width(scale * vid_w_orig);
jQuery('video').height(scale * vid_h_orig);

} }

HTML5 fullscreen video HTML5全屏视频

<!doctype html>
<html lang="en">
<head>
    <style type="text/css">
        video{
             margin: 0 auto; padding: 0;
        }
        body{
            background-color:black;
        }
    </style>
</head>
<body>

<video id="myvideo"  controls>
  <source src="movie.mp4" type="video/mp4">
  <source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>


<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script>
        $( document ).ready(function() {
            var w =  ($(window).width()) -20;
            var h = ($(window).height()) -30;

            $("#myvideo").height(h);
            $("#myvideo").width(w);
        });
        $(window).resize(function() {
            var w =  ($(window).width()) -20;
            var h = ($(window).height()) -30;

            $("#myvideo").height(h);
            $("#myvideo").width(w);
        });

    </script>

</body>
</html>

if you have the video-size, do it like this: 如果您有视频尺寸,请按照以下步骤操作:

var d = $(window),
    e = $(".secao"),
    f = e.children("div.video"),
    b, c,  j = 1920, k = 1200,
    l = d.width(),
    winH = d.height(),
    winH = winH > 600 ? winH : 600,
    c = Math.max(l / j, winH / k),
    b = Math.round(k * c) + "px";

         f.children("video").css({
            height: b
        })
 });

and set the overflow hidden. 并设置溢出隐藏。

div#intro.secao {
    background-color: #000000;
    height: 100%;
    overflow: hidden;
    position: relative;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM