forked from scerickson/covervid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
covervid.js
55 lines (45 loc) · 1.74 KB
/
covervid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
jQuery.fn.extend({
coverVid: function(width, height) {
// Call sizeVideo() on load and resize
$(document).ready(sizeVideo);
$(window).resize(sizeVideo);
// Define the attached selector
var $this = this;
function sizeVideo() {
// Get parent element height and width
var parentHeight = $this.parent().height();
var parentWidth = $this.parent().width();
// Get native video width and height
var nativeWidth = width;
var nativeHeight = height;
// Get the scale factors
var heightScaleFactor = parentHeight / nativeHeight;
var widthScaleFactor = parentWidth / nativeWidth;
// Set necessary styles to position video "center center"
$this.css({
'position': 'absolute',
'top': '50%',
'left': '50%',
'-webkit-transform': 'translate(-50%, -50%)',
'-moz-transform': 'translate(-50%, -50%)',
'-ms-transform': 'translate(-50%, -50%)',
'-o-transform': 'translate(-50%, -50%)',
'transform': 'translate(-50%, -50%)',
});
// Set overflow hidden on parent element
$this.parent().css('overflow', 'hidden');
// Based on highest scale factor set width and height
if(widthScaleFactor > heightScaleFactor) {
$this.css({
'height': 'auto',
'width': parentWidth
});
} else {
$this.css({
'height': parentHeight,
'width': 'auto'
});
}
}
}
});