|
1 | 1 | /*
|
2 |
| - * Vide - v0.2.0 |
| 2 | + * Vide - v0.2.1 |
3 | 3 | * Easy as hell jQuery plugin for video backgrounds.
|
4 | 4 | * http://vodkabears.github.io/vide/
|
5 | 5 | *
|
6 | 6 | * Made by Ilya Makarov
|
7 | 7 | * Under MIT License
|
8 | 8 | */
|
9 |
| -;(function($, window, document, navigator) { |
| 9 | +!(function($, window, document, navigator) { |
10 | 10 | "use strict";
|
11 | 11 |
|
12 | 12 | /**
|
|
37 | 37 | * @private
|
38 | 38 | */
|
39 | 39 | function parseOptions(str) {
|
40 |
| - var obj = {}, arr, i, len, prop, val, delimiterIndex, option; |
| 40 | + var obj = {}, |
| 41 | + delimiterIndex, |
| 42 | + option, |
| 43 | + prop, val, |
| 44 | + arr, len, |
| 45 | + i; |
41 | 46 |
|
42 | 47 | // remove spaces around delimiters and split
|
43 | 48 | arr = str.replace(/\s*:\s*/g, ":").replace(/\s*,\s*/g, ",").split(",");
|
|
90 | 95 | * @private
|
91 | 96 | */
|
92 | 97 | function parsePosition(str) {
|
93 |
| - // convert anything to the string |
94 | 98 | str = "" + str;
|
95 | 99 |
|
96 | 100 | // default value is a center
|
97 | 101 | var args = str.split(/\s+/),
|
98 | 102 | x = "50%", y = "50%",
|
99 |
| - i, len, arg; |
| 103 | + len, arg, |
| 104 | + i; |
100 | 105 |
|
101 | 106 | for (i = 0, len = args.length; i < len; i++) {
|
102 | 107 | arg = args[i];
|
|
153 | 158 | * @constructor
|
154 | 159 | */
|
155 | 160 | function Vide(element, path, options) {
|
156 |
| - this.element = $(element); |
157 |
| - this._defaults = defaults; |
158 |
| - this._name = pluginName; |
| 161 | + this.$element = $(element); |
159 | 162 |
|
160 | 163 | // parse path
|
161 | 164 | if (typeof path === "string") {
|
|
191 | 194 | * @public
|
192 | 195 | */
|
193 | 196 | Vide.prototype.init = function() {
|
194 |
| - var that = this, |
195 |
| - position = parsePosition(this.settings.position), |
196 |
| - poster, |
197 |
| - sources; |
| 197 | + var vide = this, |
| 198 | + position = parsePosition(vide.settings.position), |
| 199 | + sources, |
| 200 | + poster; |
198 | 201 |
|
199 | 202 | // Set video wrapper styles
|
200 |
| - this.wrapper = $("<div>").css({ |
| 203 | + vide.$wrapper = $("<div>").css({ |
201 | 204 | "position": "absolute",
|
202 | 205 | "z-index": -1,
|
203 | 206 | "top": 0,
|
|
214 | 217 | });
|
215 | 218 |
|
216 | 219 | // Get poster path
|
217 |
| - poster = this.path; |
218 |
| - if (typeof this.path === "object") { |
219 |
| - if (this.path.poster) { |
220 |
| - poster = this.path.poster; |
| 220 | + poster = vide.path; |
| 221 | + if (typeof vide.path === "object") { |
| 222 | + if (vide.path.poster) { |
| 223 | + poster = vide.path.poster; |
221 | 224 | } else {
|
222 |
| - if (this.path.mp4) { |
223 |
| - poster = this.path.mp4; |
224 |
| - } else if (this.path.webm) { |
225 |
| - poster = this.path.webm; |
226 |
| - } else if (this.path.ogv) { |
227 |
| - poster = this.path.ogv; |
| 225 | + if (vide.path.mp4) { |
| 226 | + poster = vide.path.mp4; |
| 227 | + } else if (vide.path.webm) { |
| 228 | + poster = vide.path.webm; |
| 229 | + } else if (vide.path.ogv) { |
| 230 | + poster = vide.path.ogv; |
228 | 231 | }
|
229 | 232 | }
|
230 | 233 | }
|
231 | 234 |
|
232 | 235 | // Set video poster
|
233 |
| - if (this.settings.posterType === "detect") { |
| 236 | + if (vide.settings.posterType === "detect") { |
234 | 237 | findPoster(poster, function(url) {
|
235 |
| - that.wrapper.css("background-image", "url(" + url + ")"); |
| 238 | + vide.$wrapper.css("background-image", "url(" + url + ")"); |
236 | 239 | });
|
237 |
| - } else if (this.settings.posterType !== "none") { |
238 |
| - this.wrapper |
239 |
| - .css("background-image", "url(" + poster + "." + this.settings.posterType + ")"); |
| 240 | + } else if (vide.settings.posterType !== "none") { |
| 241 | + vide.$wrapper |
| 242 | + .css("background-image", "url(" + poster + "." + vide.settings.posterType + ")"); |
240 | 243 | }
|
241 | 244 |
|
242 | 245 | // if parent element has a static position, make it relative
|
243 |
| - if (this.element.css("position") === "static") { |
244 |
| - this.element.css("position", "relative"); |
| 246 | + if (vide.$element.css("position") === "static") { |
| 247 | + vide.$element.css("position", "relative"); |
245 | 248 | }
|
246 | 249 |
|
247 |
| - this.element.prepend(this.wrapper); |
| 250 | + vide.$element.prepend(vide.$wrapper); |
248 | 251 |
|
249 | 252 | if (!isIOS && !isAndroid) {
|
250 | 253 | sources = "";
|
251 | 254 |
|
252 |
| - if (typeof this.path === "object") { |
253 |
| - if (this.path.mp4) { |
254 |
| - sources += "<source src='" + this.path.mp4 + ".mp4' type='video/mp4'>"; |
| 255 | + if (typeof vide.path === "object") { |
| 256 | + if (vide.path.mp4) { |
| 257 | + sources += "<source src='" + vide.path.mp4 + ".mp4' type='video/mp4'>"; |
255 | 258 | }
|
256 |
| - if (this.path.webm) { |
257 |
| - sources += "<source src='" + this.path.webm + ".webm' type='video/webm'>"; |
| 259 | + if (vide.path.webm) { |
| 260 | + sources += "<source src='" + vide.path.webm + ".webm' type='video/webm'>"; |
258 | 261 | }
|
259 |
| - if (this.path.ogv) { |
260 |
| - sources += "<source src='" + this.path.ogv + ".ogv' type='video/ogv'>"; |
| 262 | + if (vide.path.ogv) { |
| 263 | + sources += "<source src='" + vide.path.ogv + ".ogv' type='video/ogv'>"; |
261 | 264 | }
|
262 | 265 |
|
263 |
| - this.video = $("<video>" + sources + "</video>"); |
| 266 | + vide.$video = $("<video>" + sources + "</video>"); |
264 | 267 | } else {
|
265 |
| - this.video = $("<video>" + |
266 |
| - "<source src='" + this.path + ".mp4' type='video/mp4'>" + |
267 |
| - "<source src='" + this.path + ".webm' type='video/webm'>" + |
268 |
| - "<source src='" + this.path + ".ogv' type='video/ogg'>" + |
| 268 | + vide.$video = $("<video>" + |
| 269 | + "<source src='" + vide.path + ".mp4' type='video/mp4'>" + |
| 270 | + "<source src='" + vide.path + ".webm' type='video/webm'>" + |
| 271 | + "<source src='" + vide.path + ".ogv' type='video/ogg'>" + |
269 | 272 | "</video>");
|
270 | 273 | }
|
271 | 274 |
|
272 | 275 | // Disable visibility, while loading
|
273 |
| - this.video.css("visibility", "hidden"); |
| 276 | + vide.$video.css("visibility", "hidden"); |
274 | 277 |
|
275 | 278 | // Set video properties
|
276 |
| - this.video.prop({ |
277 |
| - autoplay: this.settings.autoplay, |
278 |
| - loop: this.settings.loop, |
279 |
| - volume: this.settings.volume, |
280 |
| - muted: this.settings.muted, |
281 |
| - playbackRate: this.settings.playbackRate |
| 279 | + vide.$video.prop({ |
| 280 | + autoplay: vide.settings.autoplay, |
| 281 | + loop: vide.settings.loop, |
| 282 | + volume: vide.settings.volume, |
| 283 | + muted: vide.settings.muted, |
| 284 | + playbackRate: vide.settings.playbackRate |
282 | 285 | });
|
283 | 286 |
|
284 | 287 | // Append video
|
285 |
| - this.wrapper.append(this.video); |
| 288 | + vide.$wrapper.append(vide.$video); |
286 | 289 |
|
287 | 290 | // Video alignment
|
288 |
| - this.video.css({ |
| 291 | + vide.$video.css({ |
289 | 292 | "margin": "auto",
|
290 | 293 | "position": "absolute",
|
291 | 294 | "z-index": -1,
|
|
297 | 300 | });
|
298 | 301 |
|
299 | 302 | // resize video, when it's loaded
|
300 |
| - this.video.bind("loadedmetadata." + pluginName, function() { |
301 |
| - that.video.css("visibility", "visible"); |
302 |
| - that.resize(); |
| 303 | + vide.$video.bind("loadedmetadata." + pluginName, function() { |
| 304 | + vide.$video.css("visibility", "visible"); |
| 305 | + vide.resize(); |
303 | 306 | });
|
304 | 307 |
|
305 | 308 | // resize event is available only for 'window',
|
306 | 309 | // use another code solutions to detect DOM elements resizing
|
307 |
| - $(this.element).bind("resize." + pluginName, function() { |
308 |
| - that.resize(); |
| 310 | + vide.$element.bind("resize." + pluginName, function() { |
| 311 | + vide.resize(); |
309 | 312 | });
|
310 | 313 | }
|
311 | 314 | };
|
|
316 | 319 | * @public
|
317 | 320 | */
|
318 | 321 | Vide.prototype.getVideoObject = function() {
|
319 |
| - return this.video ? this.video[0] : null; |
| 322 | + return this.$video ? this.$video[0] : null; |
320 | 323 | };
|
321 | 324 |
|
322 | 325 | /**
|
323 | 326 | * Resize video background
|
324 | 327 | * @public
|
325 | 328 | */
|
326 | 329 | Vide.prototype.resize = function() {
|
327 |
| - if (!this.video) { |
| 330 | + if (!this.$video) { |
328 | 331 | return;
|
329 | 332 | }
|
330 | 333 |
|
331 | 334 | var
|
332 | 335 | // get native video size
|
333 |
| - videoHeight = this.video[0].videoHeight, |
334 |
| - videoWidth = this.video[0].videoWidth, |
| 336 | + videoHeight = this.$video[0].videoHeight, |
| 337 | + videoWidth = this.$video[0].videoWidth, |
335 | 338 |
|
336 | 339 | // get wrapper size
|
337 |
| - wrapperHeight = this.wrapper.height(), |
338 |
| - wrapperWidth = this.wrapper.width(); |
| 340 | + wrapperHeight = this.$wrapper.height(), |
| 341 | + wrapperWidth = this.$wrapper.width(); |
339 | 342 |
|
340 | 343 | if (wrapperWidth / videoWidth > wrapperHeight / videoHeight) {
|
341 |
| - this.video.css({ |
| 344 | + this.$video.css({ |
342 | 345 | "width": wrapperWidth + 2, // +2 pixels to prevent empty space after transformation
|
343 | 346 | "height": "auto"
|
344 | 347 | });
|
345 | 348 | } else {
|
346 |
| - this.video.css({ |
| 349 | + this.$video.css({ |
347 | 350 | "width": "auto",
|
348 | 351 | "height": wrapperHeight + 2 // +2 pixels to prevent empty space after transformation
|
349 | 352 | });
|
|
355 | 358 | * @public
|
356 | 359 | */
|
357 | 360 | Vide.prototype.destroy = function() {
|
358 |
| - this.element.unbind(pluginName); |
359 |
| - if (this.video) { |
360 |
| - this.video.unbind(pluginName); |
| 361 | + this.$element.unbind(pluginName); |
| 362 | + |
| 363 | + if (this.$video) { |
| 364 | + this.$video.unbind(pluginName); |
361 | 365 | }
|
362 | 366 |
|
363 | 367 | delete $[pluginName].lookup[this.index];
|
364 |
| - this.element.removeData(pluginName); |
365 |
| - this.wrapper.remove(); |
| 368 | + this.$element.removeData(pluginName); |
| 369 | + this.$wrapper.remove(); |
366 | 370 | };
|
367 | 371 |
|
368 | 372 | /**
|
|
383 | 387 | */
|
384 | 388 | $.fn[pluginName] = function(path, options) {
|
385 | 389 | var instance;
|
| 390 | + |
386 | 391 | this.each(function() {
|
387 | 392 | instance = $.data(this, pluginName);
|
| 393 | + |
388 | 394 | if (instance) {
|
| 395 | + |
389 | 396 | // destroy plugin instance if exists
|
390 | 397 | instance.destroy();
|
391 | 398 | }
|
| 399 | + |
392 | 400 | // create plugin instance
|
393 | 401 | instance = new Vide(this, path, options);
|
394 | 402 | instance.index = $[pluginName].lookup.push(instance) - 1;
|
|
399 | 407 | };
|
400 | 408 |
|
401 | 409 | $(document).ready(function() {
|
| 410 | + |
402 | 411 | // window resize event listener
|
403 | 412 | $(window).bind("resize." + pluginName, function() {
|
404 |
| - for (var len = $[pluginName].lookup.length, instance, i = 0; i < len; i++) { |
| 413 | + for (var len = $[pluginName].lookup.length, i = 0, instance; i < len; i++) { |
405 | 414 | instance = $[pluginName].lookup[i];
|
| 415 | + |
406 | 416 | if (instance) {
|
407 | 417 | instance.resize();
|
408 | 418 | }
|
|
0 commit comments