vegas.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*!-----------------------------------------------------------------------------
  2. * Vegas - Fullscreen Backgrounds and Slideshows.
  3. * v2.1.2 - built 2015-04-15
  4. * Licensed under the MIT License.
  5. * http://vegas.jaysalvat.com/
  6. * ----------------------------------------------------------------------------
  7. * Copyright (C) 2010-2015 Jay Salvat
  8. * http://jaysalvat.com/
  9. * --------------------------------------------------------------------------*/
  10. (function ($) {
  11. 'use strict';
  12. var defaults = {
  13. slide: 0,
  14. delay: 5000,
  15. preload: false,
  16. preloadImage: false,
  17. preloadVideo: false,
  18. timer: true,
  19. overlay: false,
  20. autoplay: true,
  21. shuffle: false,
  22. cover: true,
  23. color: null,
  24. align: 'center',
  25. valign: 'center',
  26. transition: 'fade',
  27. transitionDuration: 1000,
  28. transitionRegister: [],
  29. animation: null,
  30. animationDuration: 'auto',
  31. animationRegister: [],
  32. init: function () {},
  33. play: function () {},
  34. pause: function () {},
  35. walk: function () {},
  36. slides: [
  37. // {
  38. // src: null,
  39. // color: null,
  40. // delay: null,
  41. // align: null,
  42. // valign: null,
  43. // transition: null,
  44. // transitionDuration: null,
  45. // animation: null,
  46. // animationDuration: null,
  47. // cover: true,
  48. // video: {
  49. // src: [],
  50. // mute: true,
  51. // loop: true
  52. // }
  53. // ...
  54. ]
  55. };
  56. var videoCache = {};
  57. var Vegas = function (elmt, options) {
  58. this.elmt = elmt;
  59. this.settings = $.extend({}, defaults, $.vegas.defaults, options);
  60. this.slide = this.settings.slide;
  61. this.total = this.settings.slides.length;
  62. this.noshow = this.total < 2;
  63. this.paused = !this.settings.autoplay || this.noshow;
  64. this.$elmt = $(elmt);
  65. this.$timer = null;
  66. this.$overlay = null;
  67. this.$slide = null;
  68. this.timeout = null;
  69. this.transitions = [
  70. 'fade', 'fade2',
  71. 'blur', 'blur2',
  72. 'flash', 'flash2',
  73. 'negative', 'negative2',
  74. 'burn', 'burn2',
  75. 'slideLeft', 'slideLeft2',
  76. 'slideRight', 'slideRight2',
  77. 'slideUp', 'slideUp2',
  78. 'slideDown', 'slideDown2',
  79. 'zoomIn', 'zoomIn2',
  80. 'zoomOut', 'zoomOut2',
  81. 'swirlLeft', 'swirlLeft2',
  82. 'swirlRight', 'swirlRight2'
  83. ];
  84. this.animations = [
  85. 'kenburns',
  86. 'kenburnsLeft', 'kenburnsRight',
  87. 'kenburnsUp', 'kenburnsUpLeft', 'kenburnsUpRight',
  88. 'kenburnsDown', 'kenburnsDownLeft', 'kenburnsDownRight'
  89. ];
  90. if (this.settings.transitionRegister instanceof Array === false) {
  91. this.settings.transitionRegister = [ this.settings.transitionRegister ];
  92. }
  93. if (this.settings.animationRegister instanceof Array === false) {
  94. this.settings.animationRegister = [ this.settings.animationRegister ];
  95. }
  96. this.transitions = this.transitions.concat(this.settings.transitionRegister);
  97. this.animations = this.animations.concat(this.settings.animationRegister);
  98. this.support = {
  99. objectFit: 'objectFit' in document.body.style,
  100. transition: 'transition' in document.body.style || 'WebkitTransition' in document.body.style,
  101. video: $.vegas.isVideoCompatible()
  102. };
  103. if (this.settings.shuffle === true) {
  104. this.shuffle();
  105. }
  106. this._init();
  107. };
  108. Vegas.prototype = {
  109. _init: function () {
  110. var $wrapper,
  111. $overlay,
  112. $timer,
  113. isBody = this.elmt.tagName === 'BODY',
  114. timer = this.settings.timer,
  115. overlay = this.settings.overlay,
  116. self = this;
  117. // Preloading
  118. this._preload();
  119. // Wrapper with content
  120. if (!isBody) {
  121. this.$elmt.css('height', this.$elmt.css('height'));
  122. $wrapper = $('<div class="vegas-wrapper">')
  123. .css('overflow', this.$elmt.css('overflow'))
  124. .css('padding', this.$elmt.css('padding'));
  125. // Some browsers don't compute padding shorthand
  126. if (!this.$elmt.css('padding')) {
  127. $wrapper
  128. .css('padding-top', this.$elmt.css('padding-top'))
  129. .css('padding-bottom', this.$elmt.css('padding-bottom'))
  130. .css('padding-left', this.$elmt.css('padding-left'))
  131. .css('padding-right', this.$elmt.css('padding-right'));
  132. }
  133. this.$elmt.clone(true).children().appendTo($wrapper);
  134. this.elmt.innerHTML = '';
  135. }
  136. // Timer
  137. if (timer && this.support.transition) {
  138. $timer = $('<div class="vegas-timer"><div class="vegas-timer-progress">');
  139. this.$timer = $timer;
  140. this.$elmt.prepend($timer);
  141. }
  142. // Overlay
  143. if (overlay) {
  144. $overlay = $('<div class="vegas-overlay">');
  145. if (typeof overlay === 'string') {
  146. $overlay.css('background-image', 'url(' + overlay + ')');
  147. }
  148. this.$overlay = $overlay;
  149. this.$elmt.prepend($overlay);
  150. }
  151. // Container
  152. this.$elmt.addClass('vegas-container');
  153. if (!isBody) {
  154. this.$elmt.append($wrapper);
  155. }
  156. setTimeout(function () {
  157. self.trigger('init');
  158. self._goto(self.slide);
  159. if (self.settings.autoplay) {
  160. self.trigger('play');
  161. }
  162. }, 1);
  163. },
  164. _preload: function () {
  165. var video, img, i;
  166. for (i = 0; i < this.settings.slides.length; i++) {
  167. if (this.settings.preload || this.settings.preloadImages) {
  168. if (this.settings.slides[i].src) {
  169. img = new Image();
  170. img.src = this.settings.slides[i].src;
  171. }
  172. }
  173. if (this.settings.preload || this.settings.preloadVideos) {
  174. if (this.support.video && this.settings.slides[i].video) {
  175. if (this.settings.slides[i].video instanceof Array) {
  176. video = this._video(this.settings.slides[i].video);
  177. } else {
  178. video = this._video(this.settings.slides[i].video.src);
  179. }
  180. }
  181. }
  182. }
  183. },
  184. _random: function (array) {
  185. return array[Math.floor(Math.random() * (array.length - 1))];
  186. },
  187. _slideShow: function () {
  188. var self = this;
  189. if (this.total > 1 && !this.paused && !this.noshow) {
  190. this.timeout = setTimeout(function () {
  191. self.next();
  192. }, this._options('delay'));
  193. }
  194. },
  195. _timer: function (state) {
  196. var self = this;
  197. clearTimeout(this.timeout);
  198. if (!this.$timer) {
  199. return;
  200. }
  201. this.$timer
  202. .removeClass('vegas-timer-running')
  203. .find('div')
  204. .css('transition-duration', '0ms');
  205. if (this.paused || this.noshow) {
  206. return;
  207. }
  208. if (state) {
  209. setTimeout(function () {
  210. self.$timer
  211. .addClass('vegas-timer-running')
  212. .find('div')
  213. .css('transition-duration', self._options('delay') - 100 + 'ms');
  214. }, 100);
  215. }
  216. },
  217. _video: function (srcs) {
  218. var video,
  219. source,
  220. cacheKey = srcs.toString();
  221. if (videoCache[cacheKey]) {
  222. return videoCache[cacheKey];
  223. }
  224. if (srcs instanceof Array === false) {
  225. srcs = [ srcs ];
  226. }
  227. video = document.createElement('video');
  228. video.preload = true;
  229. srcs.forEach(function (src) {
  230. source = document.createElement('source');
  231. source.src = src;
  232. video.appendChild(source);
  233. });
  234. videoCache[cacheKey] = video;
  235. return video;
  236. },
  237. _fadeOutSound: function (video, duration) {
  238. var self = this,
  239. delay = duration / 10,
  240. volume = video.volume - 0.09;
  241. if (volume > 0) {
  242. video.volume = volume;
  243. setTimeout(function () {
  244. self._fadeOutSound(video, duration);
  245. }, delay);
  246. } else {
  247. video.pause();
  248. }
  249. },
  250. _fadeInSound: function (video, duration) {
  251. var self = this,
  252. delay = duration / 10,
  253. volume = video.volume + 0.09;
  254. if (volume < 1) {
  255. video.volume = volume;
  256. setTimeout(function () {
  257. self._fadeInSound(video, duration);
  258. }, delay);
  259. }
  260. },
  261. _options: function (key, i) {
  262. if (i === undefined) {
  263. i = this.slide;
  264. }
  265. if (this.settings.slides[i][key] !== undefined) {
  266. return this.settings.slides[i][key];
  267. }
  268. return this.settings[key];
  269. },
  270. _goto: function (nb) {
  271. if (typeof this.settings.slides[nb] === 'undefined') {
  272. nb = 0;
  273. }
  274. this.slide = nb;
  275. var $slide,
  276. $inner,
  277. $video,
  278. $slides = this.$elmt.children('.vegas-slide'),
  279. src = this.settings.slides[nb].src,
  280. videoSettings = this.settings.slides[nb].video,
  281. delay = this._options('delay'),
  282. align = this._options('align'),
  283. valign = this._options('valign'),
  284. color = this._options('color') || this.$elmt.css('background-color'),
  285. cover = this._options('cover') ? 'cover' : 'contain',
  286. self = this,
  287. total = $slides.length,
  288. video,
  289. img;
  290. var transition = this._options('transition'),
  291. transitionDuration = this._options('transitionDuration'),
  292. animation = this._options('animation' ),
  293. animationDuration = this._options('animationDuration');
  294. if (transition === 'random' || transition instanceof Array) {
  295. if (transition instanceof Array) {
  296. transition = this._random(transition);
  297. } else {
  298. transition = this._random(this.transitions);
  299. }
  300. }
  301. if (animation === 'random' || animation instanceof Array) {
  302. if (animation instanceof Array) {
  303. animation = this._random(animation);
  304. } else {
  305. animation = this._random(this.animations);
  306. }
  307. }
  308. if (transitionDuration === 'auto' || transitionDuration > delay) {
  309. transitionDuration = delay;
  310. }
  311. if (animationDuration === 'auto') {
  312. animationDuration = delay;
  313. }
  314. $slide = $('<div class="vegas-slide"></div>');
  315. if (this.support.transition && transition) {
  316. $slide.addClass('vegas-transition-' + transition);
  317. }
  318. // Video
  319. if (this.support.video && videoSettings) {
  320. if (videoSettings instanceof Array) {
  321. video = this._video(videoSettings);
  322. } else {
  323. video = this._video(videoSettings.src);
  324. }
  325. video.loop = videoSettings.loop !== undefined ? videoSettings.loop : true;
  326. video.muted = videoSettings.mute !== undefined ? videoSettings.mute : true;
  327. if (video.muted === false) {
  328. video.volume = 0;
  329. this._fadeInSound(video, transitionDuration);
  330. } else {
  331. video.pause();
  332. }
  333. $video = $(video)
  334. .addClass('vegas-video')
  335. .css('background-color', color);
  336. if (this.support.objectFit) {
  337. $video
  338. .css('object-position', align + ' ' + valign)
  339. .css('object-fit', cover)
  340. .css('width', '100%')
  341. .css('height', '100%');
  342. } else if (cover === 'contain') {
  343. $video
  344. .css('width', '100%')
  345. .css('height', '100%');
  346. }
  347. $slide.append($video);
  348. // Image
  349. } else {
  350. img = new Image();
  351. $inner = $('<div class="vegas-slide-inner"></div>')
  352. .css('background-image', 'url(' + src + ')')
  353. .css('background-color', color)
  354. .css('background-position', align + ' ' + valign)
  355. .css('background-size', cover);
  356. if (this.support.transition && animation) {
  357. $inner
  358. .addClass('vegas-animation-' + animation)
  359. .css('animation-duration', animationDuration + 'ms');
  360. }
  361. $slide.append($inner);
  362. }
  363. if (!this.support.transition) {
  364. $slide.css('display', 'none');
  365. }
  366. if (total) {
  367. $slides.eq(total - 1).after($slide);
  368. } else {
  369. this.$elmt.prepend($slide);
  370. }
  371. self._timer(false);
  372. function go () {
  373. self._timer(true);
  374. setTimeout(function () {
  375. if (transition) {
  376. if (self.support.transition) {
  377. $slides
  378. .css('transition', 'all ' + transitionDuration + 'ms')
  379. .addClass('vegas-transition-' + transition + '-out');
  380. $slides.each(function () {
  381. var video = $slides.find('video').get(0);
  382. if (video) {
  383. video.volume = 1;
  384. self._fadeOutSound(video, transitionDuration);
  385. }
  386. });
  387. $slide
  388. .css('transition', 'all ' + transitionDuration + 'ms')
  389. .addClass('vegas-transition-' + transition + '-in');
  390. } else {
  391. $slide.fadeIn(transitionDuration);
  392. }
  393. }
  394. for (var i = 0; i < $slides.length - 4; i++) {
  395. $slides.eq(i).remove();
  396. }
  397. self.trigger('walk');
  398. self._slideShow();
  399. }, 100);
  400. }
  401. if (video) {
  402. if (video.readyState === 4) {
  403. video.currentTime = 0;
  404. }
  405. video.play();
  406. go();
  407. } else {
  408. img.src = src;
  409. img.onload = go;
  410. }
  411. },
  412. shuffle: function () {
  413. var temp,
  414. rand;
  415. for (var i = this.total - 1; i > 0; i--) {
  416. rand = Math.floor(Math.random() * (i + 1));
  417. temp = this.settings.slides[i];
  418. this.settings.slides[i] = this.settings.slides[rand];
  419. this.settings.slides[rand] = temp;
  420. }
  421. },
  422. play: function () {
  423. if (this.paused) {
  424. this.paused = false;
  425. this.next();
  426. this.trigger('play');
  427. }
  428. },
  429. pause: function () {
  430. this._timer(false);
  431. this.paused = true;
  432. this.trigger('pause');
  433. },
  434. toggle: function () {
  435. if (this.paused) {
  436. this.play();
  437. } else {
  438. this.pause();
  439. }
  440. },
  441. playing: function () {
  442. return !this.paused && !this.noshow;
  443. },
  444. current: function (advanced) {
  445. if (advanced) {
  446. return {
  447. slide: this.slide,
  448. data: this.settings.slides[this.slide]
  449. };
  450. }
  451. return this.slide;
  452. },
  453. jump: function (nb) {
  454. if (nb < 0 || nb > this.total - 1 || nb === this.slide) {
  455. return;
  456. }
  457. this.slide = nb;
  458. this._goto(this.slide);
  459. },
  460. next: function () {
  461. this.slide++;
  462. if (this.slide >= this.total) {
  463. this.slide = 0;
  464. }
  465. this._goto(this.slide);
  466. },
  467. previous: function () {
  468. this.slide--;
  469. if (this.slide < 0) {
  470. this.slide = this.total - 1;
  471. }
  472. this._goto(this.slide);
  473. },
  474. trigger: function (fn) {
  475. var params = [];
  476. if (fn === 'init') {
  477. params = [ this.settings ];
  478. } else {
  479. params = [
  480. this.slide,
  481. this.settings.slides[this.slide]
  482. ];
  483. }
  484. this.$elmt.trigger('vegas' + fn, params);
  485. if (typeof this.settings[fn] === 'function') {
  486. this.settings[fn].apply(this.$elmt, params);
  487. }
  488. },
  489. options: function (key, value) {
  490. var oldSlides = this.settings.slides.slice();
  491. if (typeof key === 'object') {
  492. this.settings = $.extend({}, defaults, $.vegas.defaults, key);
  493. } else if (typeof key === 'string') {
  494. if (value === undefined) {
  495. return this.settings[key];
  496. }
  497. this.settings[key] = value;
  498. } else {
  499. return this.settings;
  500. }
  501. // In case slides have changed
  502. if (this.settings.slides !== oldSlides) {
  503. this.total = this.settings.slides.length;
  504. this.noshow = this.total < 2;
  505. this._preload();
  506. }
  507. },
  508. destroy: function () {
  509. clearTimeout(this.timeout);
  510. this.$elmt.removeClass('vegas-container');
  511. this.$elmt.find('> .vegas-slide').remove();
  512. this.$elmt.find('> .vegas-wrapper').clone(true).children().appendTo(this.$elmt);
  513. this.$elmt.find('> .vegas-wrapper').remove();
  514. this.$timer.remove();
  515. this.$overlay.remove();
  516. this.elmt._vegas = null;
  517. }
  518. };
  519. $.fn.vegas = function(options) {
  520. var args = arguments,
  521. error = false,
  522. returns;
  523. if (options === undefined || typeof options === 'object') {
  524. return this.each(function () {
  525. if (!this._vegas) {
  526. this._vegas = new Vegas(this, options);
  527. }
  528. });
  529. } else if (typeof options === 'string') {
  530. this.each(function () {
  531. var instance = this._vegas;
  532. if (!instance) {
  533. throw new Error('No Vegas applied to this element.');
  534. }
  535. if (typeof instance[options] === 'function' && options[0] !== '_') {
  536. returns = instance[options].apply(instance, [].slice.call(args, 1));
  537. } else {
  538. error = true;
  539. }
  540. });
  541. if (error) {
  542. throw new Error('No method "' + options + '" in Vegas.');
  543. }
  544. return returns !== undefined ? returns : this;
  545. }
  546. };
  547. $.vegas = {};
  548. $.vegas.defaults = defaults;
  549. $.vegas.isVideoCompatible = function () {
  550. return !/(Android|webOS|Phone|iPad|iPod|BlackBerry|Windows Phone)/i.test(navigator.userAgent);
  551. };
  552. })(window.jQuery || window.Zepto);