vegas.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*!-----------------------------------------------------------------------------
  2. * Vegas - Fullscreen Backgrounds and Slideshows.
  3. * v2.0.0-wip - built 2015-01-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, Pin */
  11. (function ($) {
  12. 'use strict';
  13. var defaults = {
  14. slide: 0,
  15. delay: 5000,
  16. preload: true,
  17. timer: true,
  18. overlay: false,
  19. autoplay: true,
  20. shuffle: false,
  21. fill: true,
  22. color: null,
  23. align: 'center',
  24. valign: 'center',
  25. transition: 'fade',
  26. transitionDelay: 1000,
  27. init: function () {},
  28. play: function () {},
  29. pause: function () {},
  30. walk: function () {},
  31. slides: [
  32. // {
  33. // src: null,
  34. // color: null,
  35. // delay: null,
  36. // align: null,
  37. // valign: null,
  38. // transition: null,
  39. // transitiondelay: null,
  40. // fill: true,
  41. // videos: []
  42. // }
  43. // ...
  44. ]
  45. };
  46. var Vegas = function (elmt, options) {
  47. this.elmt = elmt;
  48. this.settings = $.extend({}, defaults, $.vegas.defaults, options);
  49. this.slide = this.settings.slide;
  50. this.total = this.settings.slides.length;
  51. this.noshow = this.total < 2;
  52. this.paused = !this.settings.autoplay || this.noshow;
  53. this.$elmt = $(elmt);
  54. this.$timer = null;
  55. this.$overlay = null;
  56. this.$slide = null;
  57. this.timeout = null;
  58. this.transitions = [];
  59. this.support = {
  60. objectFit: 'objectFit' in document.body.style,
  61. transition: 'transition' in document.body.style || 'WebkitTransition' in document.body.style,
  62. video: $.vegas.isVideoCompatible()
  63. };
  64. for (var i = 0; i < document.styleSheets.length; i++) {
  65. var sheet = document.styleSheets[i],
  66. rules = sheet.rules ? sheet.rules : sheet.cssRules;
  67. if (/vegas(\.min)?\.css$/.test(sheet.href)) {
  68. for (var j = 0; j < rules.length; j++) {
  69. var rule = rules[j],
  70. match = /vegas\-transition\-(.*)-|\b/gi.exec(rule.selectorText);
  71. if (match && match[1]) {
  72. this.transitions.push(match[1]);
  73. }
  74. }
  75. }
  76. }
  77. if (this.settings.shuffle === true) {
  78. this.shuffle();
  79. }
  80. this._init();
  81. };
  82. Vegas.prototype = {
  83. _init: function () {
  84. var $wrapper,
  85. $overlay,
  86. $timer,
  87. isBody = this.elmt.tagName === 'BODY',
  88. timer = this.settings.timer,
  89. overlay = this.settings.overlay,
  90. preload = this.settings.preload,
  91. img,
  92. i;
  93. // Preloading
  94. if (preload) {
  95. for (i = 0; i < this.settings.slides.length; i++) {
  96. if (this.settings.slides[i].src) {
  97. img = new Image();
  98. img.src = this.settings.slides[i].src;
  99. }
  100. }
  101. // TODO:
  102. // Preload videos
  103. }
  104. // Wrapper with content
  105. if (!isBody) {
  106. $wrapper = $('<div class="vegas-wrapper">')
  107. .css('overflow', this.$elmt.css('overflow'))
  108. .css('padding', this.$elmt.css('padding'));
  109. // Some browsers don't compute padding shorthand
  110. if (!this.$elmt.css('padding')) {
  111. $wrapper
  112. .css('padding-top', this.$elmt.css('padding-top'))
  113. .css('padding-bottom', this.$elmt.css('padding-bottom'))
  114. .css('padding-left', this.$elmt.css('padding-left'))
  115. .css('padding-right', this.$elmt.css('padding-right'));
  116. }
  117. $wrapper[0].innerHTML = this.elmt.innerHTML;
  118. this.elmt.innerHTML = '';
  119. }
  120. // Timer
  121. if (timer && this.support.transition) {
  122. $timer = $('<div class="vegas-timer"><div class="vegas-timer-progress">');
  123. this.$timer = $timer;
  124. this.$elmt.prepend($timer);
  125. }
  126. // Overlay
  127. if (overlay) {
  128. $overlay = $('<div class="vegas-overlay">');
  129. if (typeof overlay === 'string') {
  130. $overlay.css('background-image', 'url(' + overlay + ')');
  131. }
  132. this.$overlay = $overlay;
  133. this.$elmt.prepend($overlay);
  134. }
  135. // Container
  136. this.$elmt.addClass('vegas-container');
  137. if (!isBody) {
  138. this.$elmt.append($wrapper);
  139. }
  140. this.trigger('init');
  141. this._goto(this.slide);
  142. },
  143. _slideShow: function () {
  144. var self = this;
  145. if (this.total > 1 && !this.paused && !this.noshow) {
  146. this.timeout = setTimeout(function () {
  147. self.next();
  148. }, this._options('delay'));
  149. }
  150. },
  151. _timer: function (state) {
  152. var self = this;
  153. clearTimeout(this.timeout);
  154. if (!this.$timer) {
  155. return;
  156. }
  157. this.$timer
  158. .removeClass('vegas-timer-running')
  159. .find('div')
  160. .css('transition-duration', '0ms');
  161. if (this.paused || this.noshow) {
  162. return;
  163. }
  164. if (state) {
  165. setTimeout(function () {
  166. self.$timer
  167. .addClass('vegas-timer-running')
  168. .find('div')
  169. .css('transition-duration', self._options('delay') - 100 + 'ms');
  170. }, 100);
  171. }
  172. },
  173. _options: function (key, i) {
  174. if (i === undefined) {
  175. i = this.slide;
  176. }
  177. if (this.settings.slides[i][key] !== undefined) {
  178. return this.settings.slides[i][key];
  179. }
  180. return this.settings[key];
  181. },
  182. _goto: function (nb) {
  183. if (typeof this.settings.slides[nb] === 'undefined') {
  184. nb = 0;
  185. }
  186. this.slide = nb;
  187. var $slide,
  188. $slides = this.$elmt.children('.vegas-slide'),
  189. total = $slides.length,
  190. self = this,
  191. src = this.settings.slides[nb].src,
  192. videos = this.settings.slides[nb].video,
  193. delay = this._options('delay'),
  194. duration = this._options('transitionDelay'),
  195. align = this._options('align'),
  196. valign = this._options('valign'),
  197. color = this._options('color') || this.$elmt.css('background-color'),
  198. fill = this._options('fill') ? 'cover' : 'contain',
  199. transition = this._options('transition'),
  200. isRandom = transition === 'random',
  201. video,
  202. source,
  203. img;
  204. if (isRandom) {
  205. transition = this.transitions[Math.floor(Math.random() * (this.transitions.length - 1))];
  206. }
  207. if (transition !== 'none' && this.transitions.indexOf(transition) < 0) {
  208. console.error("Vegas: Transition " + transition + " doesn't exist.");
  209. }
  210. if (duration > delay) {
  211. duration = delay;
  212. }
  213. if (this.support.video && videos) {
  214. if (videos instanceof Array === false) {
  215. videos = [ videos ];
  216. }
  217. video = document.createElement('video');
  218. video.muted = true;
  219. video.loop = true;
  220. video.autoplay = true;
  221. videos.forEach(function (src) {
  222. source = document.createElement('source');
  223. source.src = src;
  224. video.appendChild(source);
  225. });
  226. $slide = $(video)
  227. .addClass('vegas-video')
  228. .addClass('vegas-slide')
  229. .addClass('vegas-transition-' + transition)
  230. .css('background-color', color);
  231. if (this.support.objectFit) {
  232. $slide
  233. .css('object-position', align + ' ' + valign)
  234. .css('object-fit', fill)
  235. .css('width', '100%')
  236. .css('height', '100%');
  237. } else if (fill === 'contain') {
  238. $slide
  239. .css('width', '100%')
  240. .css('height', '100%');
  241. }
  242. } else {
  243. img = new Image();
  244. img.src = src;
  245. $slide = $('<div></div>')
  246. .addClass('vegas-slide')
  247. .addClass('vegas-transition-' + transition)
  248. .css('background-image', 'url(' + src + ')')
  249. .css('background-color', color)
  250. .css('background-position', align + ' ' + valign)
  251. .css('background-size', fill);
  252. }
  253. if (!self.support.transition) {
  254. $slide.css('display', 'none');
  255. }
  256. if (total) {
  257. $slides.eq(total - 1).after($slide);
  258. } else {
  259. this.$elmt.prepend($slide);
  260. }
  261. $slides
  262. .css('transition', 'all 0ms')
  263. .each(function () {
  264. this.className = ' vegas-slide';
  265. this.className += ' vegas-transition-' + transition;
  266. this.className += ' vegas-transition-' + transition + '-in';
  267. if (video) {
  268. this.className += ' vegas-video';
  269. }
  270. }
  271. );
  272. self._timer(false);
  273. function go () {
  274. self._timer(true);
  275. setTimeout(function () {
  276. if (self.support.transition) {
  277. $slides
  278. .css('transition', 'all ' + duration + 'ms')
  279. .addClass('vegas-transition-' + transition + '-out');
  280. }
  281. $slide
  282. .css('transition', 'all ' + duration + 'ms')
  283. .addClass('vegas-transition-' + transition + '-in');
  284. if (!self.support.transition) {
  285. $slide.fadeIn(duration);
  286. }
  287. if ($slides.length >= 2) {
  288. $slides.eq(0).remove();
  289. }
  290. self.trigger('walk');
  291. self._slideShow();
  292. }, 100);
  293. }
  294. if (video) {
  295. // oncanplay is triggered every time when loop=true
  296. // so let's start the slide only once
  297. var played = false;
  298. video.play();
  299. video.oncanplay = function () {
  300. if (!played) {
  301. played = true;
  302. go();
  303. }
  304. };
  305. } else {
  306. img.onload = go;
  307. }
  308. },
  309. shuffle: function () {
  310. var temp,
  311. rand;
  312. for (var i = this.total - 1; i > 0; i--) {
  313. rand = Math.floor(Math.random() * (i + 1));
  314. temp = this.settings.slides[i];
  315. this.settings.slides[i] = this.settings.slides[rand];
  316. this.settings.slides[rand] = temp;
  317. }
  318. },
  319. play: function () {
  320. if (this.paused) {
  321. this.paused = false;
  322. this.next();
  323. this.trigger('play');
  324. }
  325. },
  326. pause: function () {
  327. this._timer(false);
  328. this.paused = true;
  329. this.trigger('pause');
  330. },
  331. toggle: function () {
  332. if (this.paused) {
  333. this.play();
  334. } else {
  335. this.pause();
  336. }
  337. },
  338. playing: function () {
  339. return !this.paused && !this.noshow;
  340. },
  341. current: function (advanced) {
  342. if (advanced) {
  343. return {
  344. slide: this.slide,
  345. data: this.settings.slides[this.slide]
  346. };
  347. }
  348. return this.slide;
  349. },
  350. jump: function (nb) {
  351. if (nb < 0 || nb > this.total - 1 || nb === this.slide) {
  352. return;
  353. }
  354. this.slide = nb;
  355. this._goto(this.slide);
  356. },
  357. next: function () {
  358. this.slide++;
  359. if (this.slide >= this.total) {
  360. this.slide = 0;
  361. }
  362. this._goto(this.slide);
  363. },
  364. previous: function () {
  365. this.slide--;
  366. if (this.slide < 0) {
  367. this.slide = this.total - 1;
  368. }
  369. this._goto(this.slide);
  370. },
  371. trigger: function (fn) {
  372. var params = [];
  373. if (fn !== 'init') {
  374. params = [
  375. this.slide,
  376. this.settings.slides[this.slide]
  377. ];
  378. }
  379. this.$elmt.trigger('vegas' + fn, params);
  380. if (typeof this.settings[fn] === 'function') {
  381. this.settings[fn].apply(this.$elmt, params);
  382. }
  383. },
  384. options: function (key, value) {
  385. if (typeof key === 'string') {
  386. if (value === undefined) {
  387. return this.settings[key];
  388. }
  389. this.settings[key] = value;
  390. } else if (typeof key === 'object') {
  391. this.settings = key;
  392. } else {
  393. return this.settings;
  394. }
  395. // In case slides have changed
  396. this.total = this.settings.slides.length;
  397. this.noshow = this.total < 2;
  398. }
  399. };
  400. $.fn.vegas = function(options) {
  401. var args = arguments,
  402. error = false,
  403. returns;
  404. if (options === undefined || typeof options === 'object') {
  405. return this.each(function () {
  406. if (!this._vegas) {
  407. this._vegas = new Vegas(this, options);
  408. }
  409. });
  410. } else if (typeof options === 'string') {
  411. this.each(function () {
  412. var instance = this._vegas;
  413. if (!instance) {
  414. throw new Error('No Vegas applied to this element.');
  415. }
  416. if (typeof instance[options] === 'function' && options[0] !== '_') {
  417. returns = instance[options].apply(instance, [].slice.call(args, 1));
  418. } else {
  419. error = true;
  420. }
  421. });
  422. if (error) {
  423. throw new Error('No method "' + options + '" in Vegas.');
  424. }
  425. return returns !== undefined ? returns : this;
  426. }
  427. };
  428. $.vegas = {};
  429. $.vegas.defaults = defaults;
  430. $.vegas.isVideoCompatible = function () {
  431. return !('ontouchstart' in window || 'onmsgesturechange' in window);
  432. };
  433. })(typeof jQuery !== 'undefined' ? jQuery :
  434. typeof Zepto !== 'undefined' ? Zepto :
  435. typeof Pin !== 'undefined' ? Pin : null
  436. );