vegas.js 23 KB

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