Skip to content

Instantly share code, notes, and snippets.

@bobvanluijt
Created April 6, 2016 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobvanluijt/2ca2e69aaa1703002ec733aa80bd4cbb to your computer and use it in GitHub Desktop.
Save bobvanluijt/2ca2e69aaa1703002ec733aa80bd4cbb to your computer and use it in GitHub Desktop.
Center a video element in full screen mode
/**
* Set the video sizes
*/
var videoWith = 1280,
videoHeight = 720;
/**
* The magic
*/
var videoRatio = videoHeight / videoWith,
videoNewHeight = $(window).width() * videoRatio;
if(videoNewHeight > $(window).height()){
$('video')
.css({
width : $(window).width() * 1.05,
height : ($(window).width() * 1.05) * videoRatio,
marginTop : (0 - ((($('video').height() - ($(window).height() * 1.05) ) / 2) * 1.05)) + 'px',
marginLeft: 0
})
} else {
$('video')
.css({
width : '',
height : $(window).height() * 1.05,
marginTop : 0,
marginLeft: (0 - ((($('video').width() - ($(window).width() * 1.05) ) / 2) * 1.05)) + 'px'
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment