forked from scerickson/covervid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathngCovervid.js
96 lines (83 loc) · 2.74 KB
/
ngCovervid.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**!
* NgCoverVid
* Make your HTML5 video behave like a background cover image with this lightweight Angular directive
* @author James Feigel <[email protected]>
* @version 0.1.0
*/
angular.module('ngCovervid',[])
.directive('covervid', ['$window', '$timeout', function($window, $timeout){
return {
replace: true,
restrict: 'EA',
scope: {
height: '@',
width: '@'
},
template: '<video ng-transclude></video>',
transclude: true,
link: function(scope, elem, attrs) {
function debounce(fn, delay) {
var timer = null;
return function () {
var context = this,
args = arguments;
$timeout.cancel(timer);
timer = $timeout(function () {
fn.apply(context, args);
}, delay);
};
};
var height = parseInt(scope.height);
var width = parseInt(scope.width);
if (scope.height !== undefined && isNaN(height)) {
console.error("Error: [covervid] 'height' provided is not a number. Found '" + scope.height + "'. Using native height of video.");
}
if (scope.width !== undefined && isNaN(width)) {
console.error("Error: [covervid] 'width' provided is not a number. Found '" + scope.width + "'. Using native width of video.");
}
// call sizeVideo on resize
angular.element($window).bind('resize', function() {
debounce(sizeVideo(), 50);
});
// Set necessary styles to position video "center center"
elem.css('position','absolute');
elem.css('top','50%');
elem.css('left','50%');
elem.css('-webkit-transform','translate(-50%, -50%)');
elem.css('-ms-transform','translate(-50%, -50%)');
elem.css('transform','translate(-50%, -50%)');
// Set overflow hidden on parent element
elem.parent().css('overflow','hidden');
// Define the attached selector
function sizeVideo() {
// Get parent element height and width
var parentHeight = elem.parent()[0].offsetHeight;
var parentWidth = elem.parent()[0].offsetWidth;
// Get native video height and width
var nativeHeight = height;
var nativeWidth = width;
// Get the scale factors
var heightScaleFactor = parentHeight / nativeHeight;
var widthScaleFactor = parentWidth / nativeWidth;
// Based on highest scale factor set width and height
if (widthScaleFactor > heightScaleFactor) {
elem.css('height','auto');
elem.css('width',parentWidth+'px');
} else {
elem.css('height',parentHeight+'px');
elem.css('width','auto');
}
}
if (isNaN(width) || isNaN(height)) {
elem.bind('loadedmetadata', function() {
width = elem[0].videoWidth;
height = elem[0].videoHeight;
sizeVideo();
});
}
else {
sizeVideo();
}
}
}
}]);