vegas.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*!-----------------------------------------------------------------------------
  2. * Vegas - Fullscreen Backgrounds and Slideshows.
  3. * v2.1.1 - built 2015-04-08
  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. video = this._video(this.settings.slides[i].video);
  176. }
  177. }
  178. }
  179. },
  180. _random: function (array) {
  181. return array[Math.floor(Math.random() * (array.length - 1))];
  182. },
  183. _slideShow: function () {
  184. var self = this;
  185. if (this.total > 1 && !this.paused && !this.noshow) {
  186. this.timeout = setTimeout(function () {
  187. self.next();
  188. }, this._options('delay'));
  189. }
  190. },
  191. _timer: function (state) {
  192. var self = this;
  193. clearTimeout(this.timeout);
  194. if (!this.$timer) {
  195. return;
  196. }
  197. this.$timer
  198. .removeClass('vegas-timer-running')
  199. .find('div')
  200. .css('transition-duration', '0ms');
  201. if (this.paused || this.noshow) {
  202. return;
  203. }
  204. if (state) {
  205. setTimeout(function () {
  206. self.$timer
  207. .addClass('vegas-timer-running')
  208. .find('div')
  209. .css('transition-duration', self._options('delay') - 100 + 'ms');
  210. }, 100);
  211. }
  212. },
  213. _video: function (srcs) {
  214. var video,
  215. source,
  216. cacheKey = srcs.toString();
  217. if (videoCache[cacheKey]) {
  218. return videoCache[cacheKey];
  219. }
  220. if (srcs instanceof Array === false) {
  221. srcs = [ srcs ];
  222. }
  223. video = document.createElement('video');
  224. video.preload = true;
  225. srcs.forEach(function (src) {
  226. source = document.createElement('source');
  227. source.src = src;
  228. video.appendChild(source);
  229. });
  230. videoCache[cacheKey] = video;
  231. return video;
  232. },
  233. _fadeOutSound: function (video, duration) {
  234. var self = this,
  235. delay = duration / 10,
  236. volume = video.volume - 0.09;
  237. if (volume > 0) {
  238. video.volume = volume;
  239. setTimeout(function () {
  240. self._fadeOutSound(video, duration);
  241. }, delay);
  242. } else {
  243. video.pause();
  244. }
  245. },
  246. _fadeInSound: function (video, duration) {
  247. var self = this,
  248. delay = duration / 10,
  249. volume = video.volume + 0.09;
  250. if (volume < 1) {
  251. video.volume = volume;
  252. setTimeout(function () {
  253. self._fadeInSound(video, duration);
  254. }, delay);
  255. }
  256. },
  257. _options: function (key, i) {
  258. if (i === undefined) {
  259. i = this.slide;
  260. }
  261. if (this.settings.slides[i][key] !== undefined) {
  262. return this.settings.slides[i][key];
  263. }
  264. return this.settings[key];
  265. },
  266. _goto: function (nb) {
  267. if (typeof this.settings.slides[nb] === 'undefined') {
  268. nb = 0;
  269. }
  270. this.slide = nb;
  271. var $slide,
  272. $inner,
  273. $video,
  274. $slides = this.$elmt.children('.vegas-slide'),
  275. src = this.settings.slides[nb].src,
  276. videoSettings = this.settings.slides[nb].video,
  277. delay = this._options('delay'),
  278. align = this._options('align'),
  279. valign = this._options('valign'),
  280. color = this._options('color') || this.$elmt.css('background-color'),
  281. cover = this._options('cover') ? 'cover' : 'contain',
  282. self = this,
  283. total = $slides.length,
  284. video,
  285. img;
  286. var transition = this._options('transition'),
  287. transitionDuration = this._options('transitionDuration'),
  288. animation = this._options('animation' ),
  289. animationDuration = this._options('animationDuration');
  290. if (transition === 'random' || transition instanceof Array) {
  291. if (transition instanceof Array) {
  292. transition = this._random(transition);
  293. } else {
  294. transition = this._random(this.transitions);
  295. }
  296. }
  297. if (animation === 'random' || animation instanceof Array) {
  298. if (animation instanceof Array) {
  299. animation = this._random(animation);
  300. } else {
  301. animation = this._random(this.animations);
  302. }
  303. }
  304. if (transitionDuration === 'auto' || transitionDuration > delay) {
  305. transitionDuration = delay;
  306. }
  307. if (animationDuration === 'auto') {
  308. animationDuration = delay;
  309. }
  310. $slide = $('<div class="vegas-slide"></div>');
  311. if (this.support.transition && transition) {
  312. $slide.addClass('vegas-transition-' + transition);
  313. }
  314. // Video
  315. if (this.support.video && videoSettings) {
  316. if (videoSettings instanceof Array) {
  317. video = this._video(videoSettings);
  318. } else {
  319. video = this._video(videoSettings.src);
  320. }
  321. video.loop = videoSettings.loop !== undefined ? videoSettings.loop : true;
  322. video.muted = videoSettings.mute !== undefined ? videoSettings.mute : true;
  323. if (video.muted === false) {
  324. video.volume = 0;
  325. this._fadeInSound(video, transitionDuration);
  326. } else {
  327. video.pause();
  328. }
  329. $video = $(video)
  330. .addClass('vegas-video')
  331. .css('background-color', color);
  332. if (this.support.objectFit) {
  333. $video
  334. .css('object-position', align + ' ' + valign)
  335. .css('object-fit', cover)
  336. .css('width', '100%')
  337. .css('height', '100%');
  338. } else if (cover === 'contain') {
  339. $video
  340. .css('width', '100%')
  341. .css('height', '100%');
  342. }
  343. $slide.append($video);
  344. // Image
  345. } else {
  346. img = new Image();
  347. $inner = $('<div class="vegas-slide-inner"></div>')
  348. .css('background-image', 'url(' + src + ')')
  349. .css('background-color', color)
  350. .css('background-position', align + ' ' + valign)
  351. .css('background-size', cover);
  352. if (this.support.transition && animation) {
  353. $inner
  354. .addClass('vegas-animation-' + animation)
  355. .css('animation-duration', animationDuration + 'ms');
  356. }
  357. $slide.append($inner);
  358. }
  359. if (!this.support.transition) {
  360. $slide.css('display', 'none');
  361. }
  362. if (total) {
  363. $slides.eq(total - 1).after($slide);
  364. } else {
  365. this.$elmt.prepend($slide);
  366. }
  367. self._timer(false);
  368. function go () {
  369. self._timer(true);
  370. setTimeout(function () {
  371. if (transition) {
  372. if (self.support.transition) {
  373. $slides
  374. .css('transition', 'all ' + transitionDuration + 'ms')
  375. .addClass('vegas-transition-' + transition + '-out');
  376. $slides.each(function () {
  377. var video = $slides.find('video').get(0);
  378. if (video) {
  379. video.volume = 1;
  380. self._fadeOutSound(video, transitionDuration);
  381. }
  382. });
  383. $slide
  384. .css('transition', 'all ' + transitionDuration + 'ms')
  385. .addClass('vegas-transition-' + transition + '-in');
  386. } else {
  387. $slide.fadeIn(transitionDuration);
  388. }
  389. }
  390. for (var i = 0; i < $slides.length - 4; i++) {
  391. $slides.eq(i).remove();
  392. }
  393. self.trigger('walk');
  394. self._slideShow();
  395. }, 100);
  396. }
  397. if (video) {
  398. if (video.readyState === 4) {
  399. video.currentTime = 0;
  400. }
  401. video.play();
  402. go();
  403. } else {
  404. img.src = src;
  405. img.onload = go;
  406. }
  407. },
  408. shuffle: function () {
  409. var temp,
  410. rand;
  411. for (var i = this.total - 1; i > 0; i--) {
  412. rand = Math.floor(Math.random() * (i + 1));
  413. temp = this.settings.slides[i];
  414. this.settings.slides[i] = this.settings.slides[rand];
  415. this.settings.slides[rand] = temp;
  416. }
  417. },
  418. play: function () {
  419. if (this.paused) {
  420. this.paused = false;
  421. this.next();
  422. this.trigger('play');
  423. }
  424. },
  425. pause: function () {
  426. this._timer(false);
  427. this.paused = true;
  428. this.trigger('pause');
  429. },
  430. toggle: function () {
  431. if (this.paused) {
  432. this.play();
  433. } else {
  434. this.pause();
  435. }
  436. },
  437. playing: function () {
  438. return !this.paused && !this.noshow;
  439. },
  440. current: function (advanced) {
  441. if (advanced) {
  442. return {
  443. slide: this.slide,
  444. data: this.settings.slides[this.slide]
  445. };
  446. }
  447. return this.slide;
  448. },
  449. jump: function (nb) {
  450. if (nb < 0 || nb > this.total - 1 || nb === this.slide) {
  451. return;
  452. }
  453. this.slide = nb;
  454. this._goto(this.slide);
  455. },
  456. next: function () {
  457. this.slide++;
  458. if (this.slide >= this.total) {
  459. this.slide = 0;
  460. }
  461. this._goto(this.slide);
  462. },
  463. previous: function () {
  464. this.slide--;
  465. if (this.slide < 0) {
  466. this.slide = this.total - 1;
  467. }
  468. this._goto(this.slide);
  469. },
  470. trigger: function (fn) {
  471. var params = [];
  472. if (fn === 'init') {
  473. params = [ this.settings ];
  474. } else {
  475. params = [
  476. this.slide,
  477. this.settings.slides[this.slide]
  478. ];
  479. }
  480. this.$elmt.trigger('vegas' + fn, params);
  481. if (typeof this.settings[fn] === 'function') {
  482. this.settings[fn].apply(this.$elmt, params);
  483. }
  484. },
  485. options: function (key, value) {
  486. var oldSlides = this.settings.slides.slice();
  487. if (typeof key === 'object') {
  488. this.settings = $.extend({}, defaults, $.vegas.defaults, key);
  489. } else if (typeof key === 'string') {
  490. if (value === undefined) {
  491. return this.settings[key];
  492. }
  493. this.settings[key] = value;
  494. } else {
  495. return this.settings;
  496. }
  497. // In case slides have changed
  498. if (this.settings.slides !== oldSlides) {
  499. this.total = this.settings.slides.length;
  500. this.noshow = this.total < 2;
  501. this._preload();
  502. }
  503. },
  504. destroy: function () {
  505. clearTimeout(this.timeout);
  506. this.$elmt.removeClass('vegas-container');
  507. this.$elmt.find('> .vegas-slide').remove();
  508. this.$elmt.find('> .vegas-wrapper').clone(true).children().appendTo(this.$elmt);
  509. this.$elmt.find('> .vegas-wrapper').remove();
  510. this.$timer.remove();
  511. this.$overlay.remove();
  512. this.elmt._vegas = null;
  513. }
  514. };
  515. $.fn.vegas = function(options) {
  516. var args = arguments,
  517. error = false,
  518. returns;
  519. if (options === undefined || typeof options === 'object') {
  520. return this.each(function () {
  521. if (!this._vegas) {
  522. this._vegas = new Vegas(this, options);
  523. }
  524. });
  525. } else if (typeof options === 'string') {
  526. this.each(function () {
  527. var instance = this._vegas;
  528. if (!instance) {
  529. throw new Error('No Vegas applied to this element.');
  530. }
  531. if (typeof instance[options] === 'function' && options[0] !== '_') {
  532. returns = instance[options].apply(instance, [].slice.call(args, 1));
  533. } else {
  534. error = true;
  535. }
  536. });
  537. if (error) {
  538. throw new Error('No method "' + options + '" in Vegas.');
  539. }
  540. return returns !== undefined ? returns : this;
  541. }
  542. };
  543. $.vegas = {};
  544. $.vegas.defaults = defaults;
  545. $.vegas.isVideoCompatible = function () {
  546. return !/(Android|webOS|Phone|iPad|iPod|BlackBerry|Windows Phone)/i.test(navigator.userAgent);
  547. };
  548. })(window.jQuery || window.Zepto);