jquery.vide.js
11.5 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* Vide - v0.5.1
* Easy as hell jQuery plugin for video backgrounds.
* http://vodkabears.github.io/vide/
*
* Made by Ilya Makarov
* Under MIT License
*/
!(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof exports === 'object') {
factory(require('jquery'));
} else {
factory(root.jQuery);
}
})(this, function($) {
'use strict';
/**
* Name of the plugin
* @private
* @const
* @type {String}
*/
var PLUGIN_NAME = 'vide';
/**
* Default settings
* @private
* @const
* @type {Object}
*/
var DEFAULTS = {
volume: 1,
playbackRate: 1,
muted: true,
loop: true,
autoplay: true,
position: '50% 50%',
posterType: 'detect',
resizing: true,
bgColor: 'transparent',
className: ''
};
/**
* Not implemented error message
* @private
* @const
* @type {String}
*/
var NOT_IMPLEMENTED_MSG = 'Not implemented';
/**
* Parse a string with options
* @private
* @param {String} str
* @returns {Object|String}
*/
function parseOptions(str) {
var obj = {};
var delimiterIndex;
var option;
var prop;
var val;
var arr;
var len;
var i;
// Remove spaces around delimiters and split
arr = str.replace(/\s*:\s*/g, ':').replace(/\s*,\s*/g, ',').split(',');
// Parse a string
for (i = 0, len = arr.length; i < len; i++) {
option = arr[i];
// Ignore urls and a string without colon delimiters
if (
option.search(/^(http|https|ftp):\/\//) !== -1 ||
option.search(':') === -1
) {
break;
}
delimiterIndex = option.indexOf(':');
prop = option.substring(0, delimiterIndex);
val = option.substring(delimiterIndex + 1);
// If val is an empty string, make it undefined
if (!val) {
val = undefined;
}
// Convert a string value if it is like a boolean
if (typeof val === 'string') {
val = val === 'true' || (val === 'false' ? false : val);
}
// Convert a string value if it is like a number
if (typeof val === 'string') {
val = !isNaN(val) ? +val : val;
}
obj[prop] = val;
}
// If nothing is parsed
if (prop == null && val == null) {
return str;
}
return obj;
}
/**
* Parse a position option
* @private
* @param {String} str
* @returns {Object}
*/
function parsePosition(str) {
str = '' + str;
// Default value is a center
var args = str.split(/\s+/);
var x = '50%';
var y = '50%';
var len;
var arg;
var i;
for (i = 0, len = args.length; i < len; i++) {
arg = args[i];
// Convert values
if (arg === 'left') {
x = '0%';
} else if (arg === 'right') {
x = '100%';
} else if (arg === 'top') {
y = '0%';
} else if (arg === 'bottom') {
y = '100%';
} else if (arg === 'center') {
if (i === 0) {
x = '50%';
} else {
y = '50%';
}
} else {
if (i === 0) {
x = arg;
} else {
y = arg;
}
}
}
return { x: x, y: y };
}
/**
* Search a poster
* @private
* @param {String} path
* @param {Function} callback
*/
function findPoster(path, callback) {
var onLoad = function() {
callback(this.src);
};
}
/**
* Vide constructor
* @param {HTMLElement} element
* @param {Object|String} path
* @param {Object|String} options
* @constructor
*/
function Vide(element, path, options) {
this.$element = $(element);
// Parse path
if (typeof path === 'string') {
path = parseOptions(path);
}
// Parse options
if (!options) {
options = {};
} else if (typeof options === 'string') {
options = parseOptions(options);
}
// Remove an extension
if (typeof path === 'string') {
path = path.replace(/\.\w*$/, '');
} else if (typeof path === 'object') {
for (var i in path) {
if (path.hasOwnProperty(i)) {
path[i] = path[i].replace(/\.\w*$/, '');
}
}
}
this.settings = $.extend({}, DEFAULTS, options);
this.path = path;
// https://github.com/VodkaBears/Vide/issues/110
try {
this.init();
} catch (e) {
if (e.message !== NOT_IMPLEMENTED_MSG) {
throw e;
}
}
}
/**
* Initialization
* @public
*/
Vide.prototype.init = function() {
var vide = this;
var path = vide.path;
var poster = path;
var sources = '';
var $element = vide.$element;
var settings = vide.settings;
var position = parsePosition(settings.position);
var posterType = settings.posterType;
var $video;
var $wrapper;
// Set styles of a video wrapper
$wrapper = vide.$wrapper = $('<div>')
.addClass(settings.className)
.css({
position: 'absolute',
'z-index': -1,
top: 0,
left: 0,
bottom: 0,
right: 0,
overflow: 'hidden',
'-webkit-background-size': 'cover',
'-moz-background-size': 'cover',
'-o-background-size': 'cover',
'background-size': 'cover',
'background-color': settings.bgColor,
'background-repeat': 'no-repeat',
'background-position': position.x + ' ' + position.y
});
// Get a poster path
if (typeof path === 'object') {
if (path.poster) {
poster = path.poster;
} else {
if (path.mp4) {
poster = path.mp4;
} else if (path.webm) {
poster = path.webm;
} else if (path.ogv) {
poster = path.ogv;
}
}
}
// Set a video poster
if (posterType === 'detect') {
findPoster(poster, function(url) {
$wrapper.css('background-image', 'url(' + url + ')');
});
} else if (posterType !== 'none') {
$wrapper.css('background-image', 'url(' + poster + '.' + posterType + ')');
}
// If a parent element has a static position, make it relative
if ($element.css('position') === 'static') {
$element.css('position', 'static');
}
$element.prepend($wrapper);
if (typeof path === 'object') {
if (path.mp4) {
sources += '<source src="' + path.mp4 + '.mp4" type="video/mp4">';
}
if (path.webm) {
sources += '<source src="' + path.webm + '.webm" type="video/webm">';
}
if (path.ogv) {
sources += '<source src="' + path.ogv + '.ogv" type="video/ogg">';
}
$video = vide.$video = $('<video>' + sources + '</video>');
} else {
$video = vide.$video = $('<video>' +
'<source src="' + path + '.mp4" type="video/mp4">' +
'<source src="' + path + '.webm" type="video/webm">' +
'<source src="' + path + '.ogv" type="video/ogg">' +
'</video>');
}
// https://github.com/VodkaBears/Vide/issues/110
try {
$video
// Set video properties
.prop({
autoplay: settings.autoplay,
loop: settings.loop,
volume: settings.volume,
muted: settings.muted,
defaultMuted: settings.muted,
playbackRate: settings.playbackRate,
defaultPlaybackRate: settings.playbackRate
});
} catch (e) {
throw new Error(NOT_IMPLEMENTED_MSG);
}
// Video alignment
$video.css({
margin: 'auto',
position: 'absolute',
'z-index': -1,
top: position.y,
left: position.x,
'-webkit-transform': 'translate(-' + position.x + ', -' + position.y + ')',
'-ms-transform': 'translate(-' + position.x + ', -' + position.y + ')',
'-moz-transform': 'translate(-' + position.x + ', -' + position.y + ')',
transform: 'translate(-' + position.x + ', -' + position.y + ')',
// Disable visibility, while loading
visibility: 'hidden',
opacity: 0
})
// Resize a video, when it's loaded
.one('canplaythrough.' + PLUGIN_NAME, function() {
vide.resize();
})
// Make it visible, when it's already playing
.one('playing.' + PLUGIN_NAME, function() {
$video.css({
visibility: 'visible',
opacity: 1
});
$wrapper.css('background-image', 'none');
});
// Resize event is available only for 'window'
// Use another code solutions to detect DOM elements resizing
$element.on('resize.' + PLUGIN_NAME, function() {
if (settings.resizing) {
vide.resize();
}
});
// Append a video
$wrapper.append($video);
};
/**
* Get a video element
* @public
* @returns {HTMLVideoElement}
*/
Vide.prototype.getVideoObject = function() {
return this.$video[0];
};
/**
* Resize a video background
* @public
*/
Vide.prototype.resize = function() {
if (!this.$video) {
return;
}
var $wrapper = this.$wrapper;
var $video = this.$video;
var video = $video[0];
// Get a native video size
var videoHeight = video.videoHeight;
var videoWidth = video.videoWidth;
// Get a wrapper size
var wrapperHeight = $wrapper.height();
var wrapperWidth = $wrapper.width();
if (wrapperWidth / videoWidth > wrapperHeight / videoHeight) {
$video.css({
// +2 pixels to prevent an empty space after transformation
width: wrapperWidth + 2,
height: 'auto'
});
} else {
$video.css({
width: 'auto',
// +2 pixels to prevent an empty space after transformation
height: wrapperHeight + 2
});
}
};
/**
* Destroy a video background
* @public
*/
Vide.prototype.destroy = function() {
delete $[PLUGIN_NAME].lookup[this.index];
this.$video && this.$video.off(PLUGIN_NAME);
this.$element.off(PLUGIN_NAME).removeData(PLUGIN_NAME);
this.$wrapper.remove();
};
/**
* Special plugin object for instances.
* @public
* @type {Object}
*/
$[PLUGIN_NAME] = {
lookup: []
};
/**
* Plugin constructor
* @param {Object|String} path
* @param {Object|String} options
* @returns {JQuery}
* @constructor
*/
$.fn[PLUGIN_NAME] = function(path, options) {
var instance;
this.each(function() {
instance = $.data(this, PLUGIN_NAME);
// Destroy the plugin instance if exists
instance && instance.destroy();
// Create the plugin instance
instance = new Vide(this, path, options);
instance.index = $[PLUGIN_NAME].lookup.push(instance) - 1;
$.data(this, PLUGIN_NAME, instance);
});
return this;
};
$(document).ready(function() {
var $window = $(window);
// Window resize event listener
$window.on('resize.' + PLUGIN_NAME, function() {
for (var len = $[PLUGIN_NAME].lookup.length, i = 0, instance; i < len; i++) {
instance = $[PLUGIN_NAME].lookup[i];
if (instance && instance.settings.resizing) {
instance.resize();
}
}
});
// https://github.com/VodkaBears/Vide/issues/68
$window.on('unload.' + PLUGIN_NAME, function() {
return false;
});
// Auto initialization
// Add 'data-vide-bg' attribute with a path to the video without extension
// Also you can pass options throw the 'data-vide-options' attribute
// 'data-vide-options' must be like 'muted: false, volume: 0.5'
$(document).find('[data-' + PLUGIN_NAME + '-bg]').each(function(i, element) {
var $element = $(element);
var options = $element.data(PLUGIN_NAME + '-options');
var path = $element.data(PLUGIN_NAME + '-bg');
$element[PLUGIN_NAME](path, options);
});
});
});