vegas.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. 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. if (typeof this.settings.slides[nb] === 'undefined') {
  185. nb = 0;
  186. }
  187. this.slide = nb;
  188. var $slide,
  189. $slides = this.$elmt.children('.vegas-slide'),
  190. total = $slides.length,
  191. self = this,
  192. src = this.settings.slides[nb].src,
  193. videos = this.settings.slides[nb].video,
  194. delay = this._options('delay'),
  195. duration = this._options('transitionDelay'),
  196. align = this._options('align'),
  197. valign = this._options('valign'),
  198. color = this._options('color') || this.$elmt.css('background-color'),
  199. fill = this._options('fill') ? 'cover' : 'contain',
  200. transition = this._options('transition'),
  201. isRandom = transition === 'random',
  202. video,
  203. source,
  204. img;
  205. if (isRandom) {
  206. transition = this.transitions[Math.floor(Math.random() * (this.transitions.length - 1))];
  207. }
  208. if (transition !== 'none' && this.transitions.indexOf(transition) < 0) {
  209. console.error("Vegas: Transition " + transition + " doesn't exist.");
  210. }
  211. if (duration > delay) {
  212. duration = delay;
  213. }
  214. if (this.support.video && videos) {
  215. if (videos instanceof Array === false) {
  216. videos = [ videos ];
  217. }
  218. video = document.createElement('video');
  219. video.muted = true;
  220. video.loop = true;
  221. video.autoplay = true;
  222. videos.forEach(function (src) {
  223. source = document.createElement('source');
  224. source.src = src;
  225. video.appendChild(source);
  226. });
  227. $slide = $(video)
  228. .addClass('vegas-video')
  229. .addClass('vegas-slide')
  230. .addClass('vegas-transition-' + transition)
  231. .css('background-color', color);
  232. if (this.support.objectFit) {
  233. $slide
  234. .css('object-position', align + ' ' + valign)
  235. .css('object-fit', fill)
  236. .css('width', '100%')
  237. .css('height', '100%');
  238. } else if (fill === 'contain') {
  239. $slide
  240. .css('width', '100%')
  241. .css('height', '100%');
  242. }
  243. } else {
  244. img = new Image();
  245. img.src = src;
  246. $slide = $('<div></div>')
  247. .addClass('vegas-slide')
  248. .addClass('vegas-transition-' + transition)
  249. .css('background-image', 'url(' + src + ')')
  250. .css('background-color', color)
  251. .css('background-position', align + ' ' + valign)
  252. .css('background-size', fill);
  253. }
  254. if (!self.support.transition) {
  255. $slide.css('display', 'none');
  256. }
  257. if (total) {
  258. $slides.eq(total - 1).after($slide);
  259. } else {
  260. this.$elmt.prepend($slide);
  261. }
  262. $slides
  263. .css('transition', 'all 0ms')
  264. .each(function () {
  265. this.className = ' vegas-slide';
  266. this.className += ' vegas-transition-' + transition;
  267. this.className += ' vegas-transition-' + transition + '-in';
  268. if (video) {
  269. this.className += ' vegas-video';
  270. }
  271. }
  272. );
  273. self._timer(false);
  274. function go () {
  275. self._timer(true);
  276. setTimeout(function () {
  277. if (self.support.transition) {
  278. $slides
  279. .css('transition', 'all ' + duration + 'ms')
  280. .addClass('vegas-transition-' + transition + '-out');
  281. }
  282. $slide
  283. .css('transition', 'all ' + duration + 'ms')
  284. .addClass('vegas-transition-' + transition + '-in');
  285. if (!self.support.transition) {
  286. $slide.fadeIn(duration);
  287. }
  288. if ($slides.length >= 2) {
  289. $slides.eq(0).remove();
  290. }
  291. self.trigger('walk');
  292. self._slideShow();
  293. }, 100);
  294. }
  295. if (video) {
  296. // oncanplay is triggered every time when loop=true
  297. // so let's start the slide only once
  298. var played = false;
  299. video.play();
  300. video.oncanplay = function () {
  301. if (!played) {
  302. played = true;
  303. go();
  304. }
  305. };
  306. } else {
  307. img.onload = go;
  308. }
  309. },
  310. shuffle: function () {
  311. var temp,
  312. rand;
  313. for (var i = this.total - 1; i > 0; i--) {
  314. rand = Math.floor(Math.random() * (i + 1));
  315. temp = this.settings.slides[i];
  316. this.settings.slides[i] = this.settings.slides[rand];
  317. this.settings.slides[rand] = temp;
  318. }
  319. },
  320. play: function () {
  321. if (this.paused) {
  322. this.paused = false;
  323. this.next();
  324. this.trigger('play');
  325. }
  326. },
  327. pause: function () {
  328. this._timer(false);
  329. this.paused = true;
  330. this.trigger('pause');
  331. },
  332. toggle: function () {
  333. if (this.paused) {
  334. this.play();
  335. } else {
  336. this.pause();
  337. }
  338. },
  339. playing: function () {
  340. return !this.paused && !this.noshow;
  341. },
  342. current: function (advanced) {
  343. if (advanced) {
  344. return {
  345. slide: this.slide,
  346. data: this.settings.slides[this.slide]
  347. };
  348. }
  349. return this.slide;
  350. },
  351. jump: function (nb) {
  352. if (nb < 0 || nb > this.total - 1 || nb === this.slide) {
  353. return;
  354. }
  355. this.slide = nb;
  356. this._goto(this.slide);
  357. },
  358. next: function () {
  359. this.slide++;
  360. if (this.slide >= this.total) {
  361. this.slide = 0;
  362. }
  363. this._goto(this.slide);
  364. },
  365. previous: function () {
  366. this.slide--;
  367. if (this.slide < 0) {
  368. this.slide = this.total - 1;
  369. }
  370. this._goto(this.slide);
  371. },
  372. trigger: function (fn) {
  373. var params = [];
  374. if (fn !== 'init') {
  375. params = [
  376. this.slide,
  377. this.settings.slides[this.slide]
  378. ];
  379. }
  380. this.$elmt.trigger('vegas' + fn, params);
  381. if (typeof this.settings[fn] === 'function') {
  382. this.settings[fn].apply(this.$elmt, params);
  383. }
  384. },
  385. options: function (key, value) {
  386. if (typeof key === 'string') {
  387. if (value === undefined) {
  388. return this.settings[key];
  389. }
  390. this.settings[key] = value;
  391. } else if (typeof key === 'object') {
  392. this.settings = key;
  393. } else {
  394. return this.settings;
  395. }
  396. }
  397. };
  398. $.fn.vegas = function(options) {
  399. var args = arguments,
  400. error = false,
  401. returns;
  402. if (options === undefined || typeof options === 'object') {
  403. return this.each(function () {
  404. if (!this._vegas) {
  405. this._vegas = new Vegas(this, options);
  406. }
  407. });
  408. } else if (typeof options === 'string') {
  409. this.each(function () {
  410. var instance = this._vegas;
  411. if (!instance) {
  412. throw new Error('No Vegas applied to this element.');
  413. }
  414. if (typeof instance[options] === 'function' && options[0] !== '_') {
  415. returns = instance[options].apply(instance, [].slice.call(args, 1));
  416. } else {
  417. error = true;
  418. }
  419. });
  420. if (error) {
  421. throw new Error('No method "' + options + '" in Vegas.');
  422. }
  423. return returns !== undefined ? returns : this;
  424. }
  425. };
  426. $.vegas = {};
  427. $.vegas.defaults = defaults;
  428. $.vegas.isVideoCompatible = function () {
  429. return !('ontouchstart' in window || 'onmsgesturechange' in window);
  430. };
  431. })(typeof jQuery !== 'undefined' ? jQuery :
  432. typeof Zepto !== 'undefined' ? Zepto :
  433. typeof Pin !== 'undefined' ? Pin : null
  434. );