vegas.js 23 KB

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