vegas.js 16 KB

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