vegas.js 17 KB

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