vegas.js 20 KB

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