forked from cyberjo50/backgroundVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backgroundVideo.js
248 lines (208 loc) · 8.69 KB
/
backgroundVideo.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*!
* backgroundVideo v0.1.3
* https://github.com/linnett/backgroundVideo
* Use HTML5 video to create an effect like the CSS property, 'background-size: cover'. Includes parallax option.
*
* Copyright 2014 Sam Linnett
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @license http://www.gnu.org/licenses/gpl.html GPL2 License
*
*/
;(function ( $, window, document, undefined ) {
"use strict";
// Create the defaults once
var pluginName = "backgroundVideo",
defaults = {
$videoWrap: $('#video-wrap'),
$outerWrap: $(window),
$window: $(window),
minimumVideoWidth: 400,
preventContextMenu: false,
parallax: true,
parallaxOptions: {
effect: 1.5
},
pauseVideoOnViewLoss: false
};
// The actual plugin constructor
function Plugin( element, options ) {
var me = this;
this.element = element;
this.options = $.extend( {}, defaults, options );
this._defaults = defaults;
this._name = pluginName;
this.options.$video = $(element);
this.detectBrowser();
this.options.has3d = this.detect3d();
// Set wrap default styles
this.options.$videoWrap.css({
'position': 'relative',
'overflow': 'hidden',
'z-index': '10'
});
// Set object default styles
this.options.$video.css({
'position': 'absolute',
'z-index': '1'
});
this.options.$video.on('canplay canplaythrough', readyCallback);
// If video is cached, the video will already be ready
// so canplay/canplaythrough may not fire.
if (this.options.$video[0].readyState > 3) {
readyCallback();
}
function readyCallback() {
me.options.originalVideoW = me.options.$video[0].videoWidth;
me.options.originalVideoH = me.options.$video[0].videoHeight;
if(me.initialised) {
return;
}
me.init();
}
}
Plugin.prototype = {
init: function() {
var me = this;
this.initialised = true;
// Run scaleObject function on window resize
this.options.$window.resize(function() {
me.positionObject(me.options.$video);
});
// Use Parallax effect on the object
if(this.options.parallax) {
// Set scroll listener for parallax effect
this.options.$window.on('scroll.backgroundVideoParallax', function () {
me.positionObject(me.options.$video);
});
}
// Pause video when the video goes out of the browser view
if(this.options.pauseVideoOnViewLoss) {
this.playPauseVideo();
}
// Prevent context menu on right click for object
if(this.options.preventContextMenu) {
this.options.$video.on('contextmenu', function() { return false; });
}
// Prompt resize to trigger listeners and set to browser size
this.options.$window.trigger('resize');
},
detect3d: function () {
var el = document.createElement('p'), t, has3d,
transforms = {
'WebkitTransform':'-webkit-transform',
'OTransform':'-o-transform',
'MSTransform':'-ms-transform',
'MozTransform':'-moz-transform',
'transform':'transform'
};
document.body.insertBefore(el, document.body.lastChild);
for(t in transforms){
if( el.style[t] !== undefined ){
el.style[t] = 'matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)';
has3d = window.getComputedStyle(el).getPropertyValue( transforms[t] );
}
}
if( has3d !== undefined ){
return has3d !== 'none';
} else {
return false;
}
},
detectBrowser: function () {
var val = navigator.userAgent.toLowerCase();
if( val.indexOf('chrome') > -1 || val.indexOf('safari') > -1 ) {
this.options.browser = 'webkit';
this.options.browserPrexix = '-webkit-';
}
else if( val.indexOf('firefox') > -1 ) {
this.options.browser = 'firefox';
this.options.browserPrexix = '-moz-';
}
else if (val.indexOf('MSIE') !== -1 || val.indexOf('Trident/') > 0) {
this.options.browser = 'ie';
this.options.browserPrexix = '-ms-';
}
else if( val.indexOf('Opera') > -1 ) {
this.options.browser = 'opera';
this.options.browserPrexix = '-o-';
}
},
scaleObject: function($video, $videoWrap) {
var me = this, heightScale, widthScale, scaleFactor;
// Set the video wrap to the outerWrap size (defaulted to window)
$videoWrap.width(this.options.$outerWrap.width());
$videoWrap.height(this.options.$outerWrap.height());
heightScale = this.options.$window.width() / this.options.originalVideoW;
widthScale = this.options.$window.height() / this.options.originalVideoH;
scaleFactor = heightScale > widthScale ? heightScale : widthScale;
if (scaleFactor * this.options.originalVideoW < this.options.minimumVideoWidth) {
scaleFactor = this.options.minimumVideoWidth / this.options.originalVideoW;
}
// Scale object
$video.width(scaleFactor * this.options.originalVideoW);
$video.height(scaleFactor * this.options.originalVideoH);
return {
// Return x and y axis values for positioning
xPos: -(parseInt($video.width() - this.options.$window.width()) / 2),
yPos: parseInt($video.height() - this.options.$window.height()) / 2
};
},
positionObject: function($video) {
var me = this,
scrollPos = this.options.$window.scrollTop(),
scaleObject = this.scaleObject($video, me.options.$videoWrap),
xPos = scaleObject.xPos,
yPos = scaleObject.yPos;
// Check for parallax
if(this.options.parallax) {
// Prevent parallax when scroll position is negative to the window
if(scrollPos >= 0) {
yPos = this.calculateYPos(yPos,scrollPos);
} else {
yPos = this.calculateYPos(yPos, 0);
}
} else {
yPos = -yPos;
}
// Check for 3dtransforms
if(me.options.has3d) {
$video.css(me.options.browserPrexix + 'transform3d', 'translate3d(-'+ xPos +'px, ' + yPos + 'px, 0)');
$video.css('transform', 'translate3d('+ xPos +'px, ' + yPos + 'px, 0)');
} else {
$video.css(me.options.browserPrexix + 'transform', 'translate(-'+ xPos +'px, ' + yPos + 'px)');
$video.css('transform', 'translate('+ xPos +'px, ' + yPos + 'px)');
}
},
calculateYPos: function (yPos, scrollPos) {
var videoPosition, videoOffset;
videoPosition = parseInt(this.options.$videoWrap.offset().top);
videoOffset = videoPosition - scrollPos;
yPos = -((videoOffset / this.options.parallaxOptions.effect) + yPos);
return yPos;
},
disableParallax: function () {
this.options.$window.unbind('.backgroundVideoParallax');
},
playPauseVideo: function () {
var me = this;
this.options.$window.on('scroll.backgroundVideoPlayPause', function () {
// Play/Pause video depending on where the user is in the browser
if(me.options.$window.scrollTop() < me.options.$videoWrap.height()) {
me.options.$video.get(0).play();
} else {
me.options.$video.get(0).pause();
}
});
}
};
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName,
new Plugin( this, options ));
}
});
};
})( jQuery, window, document );