vegas.js 16 KB

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