math - FULL bleed video, keep aspect ratio, center, pure javascript -
i have element keep aspect ratio act 'full-bleed' or rather full browser experience. video should or down accommodate new browser window dimensions, centered in browser window. @ times, video cropped – that's ok me. trying recreate background cover css property video element using pure js, if helps imagine it. not know is, heres following:
.bkgdcover{ background: no-repeat center center; -webkit-background-size: 100% 100%; -moz-background-size: cover; -o-background-size: cover; background-size: cover; } the following attempt same in pure javascript:
function getwinsize(){ var wid = 0, hei = 0; if (document.documentelement && document.documentelement.clientheight){ wid = parseint(window.innerwidth,10); hei = parseint(window.innerheight,10); } else if (document.body){ wid = parseint(document.body.offsetwidth,10); hei = parseint(document.body.offsetheight,10); } return [wid,hei]; } this.resize2 = function(){ fullbleedvideoplayer(); }; function fullbleedvideoplayer() { var viewport=getwinsize(); var videoratio = 1080 / 1920; var browserheight = viewport[0]; var browserwidth = viewport[1]; var tmp_windowratio = viewport[1] / viewport[0]; if (videoratio > tmp_windowratio) { tmp_height = math.round(browserwidth * videoratio); _player.style.width= browserwidth+ "px"; _player.style.height= tmp_height+ "px"; _player.style.marginleft = 0 + 'px'; _player.style.margintop = -math.round((tmp_height - browserheight) / 2) + 'px'; } else { tmp_width = math.round(browserheight * (1 / videoratio)); _player.style.width= tmp_width+ "px"; _player.style.height= browserheight+ "px"; _player.style.marginleft = -math.round((tmp_width - browserwidth) / 2) + 'px'; _player.style.margintop = 0 + 'px'; } } unfortunately, not reproduce css cover. if can see made mistake or have done same yourself, i'd love hear you. pure js solutions please.
i did except wasn't full screen, background video of page header had height of 450px'ish'. ish because i'm using 25vw , it's changing height based on viewport width. wanted video stay centered, how?
html
<section id="homepage--hero"> <div class="fullpage-video-wrapper"> <video id="video_background" preload="auto" autoplay="true" loop="loop" muted="muted" volume="0"> <source src="http://vjs.zencdn.net/v/oceans.mp4" type="video/mp4"> <source src="http://vjs.zencdn.net/v/oceans.webm" type="video/webm"> <!-- doesnt support video --> <img src="#" style="width: 100%; height: auto;" /> </video> </div> </section> css
#homepage-hero { display: block; height: 35vw; width: 100%; position: relative; overflow: hidden; } .fullpage-video-wrapper { position: absolute; bottom: 0px; left: 0px; min-width: 100%; min-height: 100%; width: auto; height: auto; z-index: -1000; overflow: hidden; } .fullpage-video-wrapper { min-height: 100%; min-width: 100%; } js
if( $('.fullpage-video-wrapper').length > 0 ) { var videowrapper = $('.fullpage-video-wrapper'), videoparentwrapper = $(videowrapper).parent(); if($(videowrapper).height() > $(videoparentwrapper).height() ) { var difference = ($(videowrapper).height() - $(videoparentwrapper).height()) / 4 $(videowrapper).css({ bottom: -parseint(difference) }); } $(window).resize(function(){ if($(videowrapper).height() > $(videoparentwrapper).height() ) { var difference = ($(videowrapper).height() - $(videoparentwrapper).height()) / 4 $(videowrapper).css({ bottom: -parseint(difference) }); } }); } what makes video stay same aspect ratio when resizing window, positions bottom of homepage-hero, caused respond without causing see background color of video-wrapper, not centered. jquery setting videowrapper offset difference in height of video wrapper element height , it's parent elements height. allowing aspect ratio stay same, bleeding video when needed allowing centered in it's parents element vertically.
this super rough, since working on hour ago, i'm sure theres plenty need rework through , dialed in, helps along path, should same solution except doing height checks on video element or video wrap element outerheight , viewport height.
Comments
Post a Comment