vegas.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /*!-----------------------------------------------------------------------------
  2. * Vegas - Fullscreen Backgrounds and Slideshows.
  3. * v2.5.4 - built 2021-01-30
  4. * Licensed under the MIT License.
  5. * http://vegas.jaysalvat.com/
  6. * ----------------------------------------------------------------------------
  7. * Copyright (C) 2010-2021 Jay Salvat
  8. * http://jaysalvat.com/
  9. * --------------------------------------------------------------------------*/
  10. /* Customize By Hugh
  11. * version 1.0.2 - 20201221
  12. * Simple use by just div[type=vegas] image ...
  13. */
  14. (function($) {
  15. 'use strict';
  16. var defaults = {
  17. slide: 0,
  18. delay: 5000,
  19. loop: true,
  20. preload: false,
  21. preloadImage: false,
  22. preloadVideo: false,
  23. timer: true,
  24. overlay: false,
  25. autoplay: true,
  26. shuffle: false,
  27. cover: true,
  28. color: null,
  29. align: 'center',
  30. valign: 'center',
  31. firstTransition: null,
  32. firstTransitionDuration: null,
  33. transition: 'fade',
  34. transitionDuration: 1000,
  35. transitionRegister: [],
  36. animation: null,
  37. animationDuration: 'auto',
  38. animationRegister: [],
  39. slidesToKeep: 1,
  40. "arrow-status": 'none', // none|disabled|hide|show
  41. "arrow-hookStatus": 'hide', // none|disabled|hide|show
  42. "arrow-prevHook": null,
  43. "arrow-nextHook": null,
  44. "dot-status": 'none', //none|show
  45. "dot-hook": null,
  46. "dot-hookStatus": 'clone', // none|clone|clone-id
  47. prevTransparent: true,
  48. init: function() {},
  49. play: function() {},
  50. pause: function() {},
  51. walk: function() {},
  52. slides: [// {
  53. // src: null,
  54. // color: null,
  55. // delay: null,
  56. // align: null,
  57. // valign: null,
  58. // transition: null,
  59. // transitionDuration: null,
  60. // animation: null,
  61. // animationDuration: null,
  62. // cover: true,
  63. // video: {
  64. // src: [],
  65. // mute: true,
  66. // loop: true
  67. // }
  68. // ...
  69. ]
  70. };
  71. var videoCache = {};
  72. var Vegas = function(elmt, options) {
  73. this.elmt = elmt;
  74. this.$elmt = $(elmt);
  75. this.settings = $.extend({}, defaults, $.vegas.defaults, options);
  76. this._init_settings();
  77. this.slide = this.settings.slide;
  78. this.total = this.settings.slides.length;
  79. this.noshow = this.total < 2;
  80. this.paused = !this.settings.autoplay || this.noshow;
  81. this.ended = false;
  82. this.$timer = null;
  83. this.$overlay = null;
  84. this.$slide = null;
  85. this.timeout = null;
  86. this.first = true;
  87. this.transitions = ['fade', 'fade2', 'blur', 'blur2', 'flash', 'flash2', 'negative', 'negative2', 'burn', 'burn2', 'slideLeft', 'slideLeft2', 'slideRight', 'slideRight2', 'slideUp', 'slideUp2', 'slideDown', 'slideDown2', 'zoomIn', 'zoomIn2', 'zoomOut', 'zoomOut2', 'swirlLeft', 'swirlLeft2', 'swirlRight', 'swirlRight2'];
  88. this.animations = ['kenburns', 'kenburnsLeft', 'kenburnsRight', 'kenburnsUp', 'kenburnsUpLeft', 'kenburnsUpRight', 'kenburnsDown', 'kenburnsDownLeft', 'kenburnsDownRight'];
  89. if (!(this.settings.transitionRegister instanceof Array)) {
  90. this.settings.transitionRegister = [this.settings.transitionRegister];
  91. }
  92. if (!(this.settings.animationRegister instanceof Array)) {
  93. this.settings.animationRegister = [this.settings.animationRegister];
  94. }
  95. this.transitions = this.transitions.concat(this.settings.transitionRegister);
  96. this.animations = this.animations.concat(this.settings.animationRegister);
  97. this.support = {
  98. objectFit: 'objectFit'in document.body.style,
  99. transition: 'transition'in document.body.style || 'WebkitTransition'in document.body.style,
  100. video: $.vegas.isVideoCompatible()
  101. };
  102. if (this.settings.shuffle === true) {
  103. this.shuffle();
  104. }
  105. this._init();
  106. this._arrows();
  107. this._dots();
  108. };
  109. Vegas.prototype = {
  110. _init_settings: function(options) {
  111. var attributes = this.elmt.attributes;
  112. var settings = this.settings;
  113. window.attr_merge(attributes, settings);
  114. this.slide = settings.slide;
  115. var slides = [];
  116. this.$elmt.children().each(function() {
  117. var $this = $(this);
  118. if ($this.is('img')) {
  119. slides.push({
  120. src: $this.attr('src'),
  121. });
  122. if ($this.attr('data-transition') != undefined && $this.attr('data-transition') != settings['transition']) {
  123. slides[slides.length - 1].transition = $this.attr('data-transition');
  124. }
  125. if ($this.attr('data-animation') != undefined && $this.attr('data-animation') != settings['animation']) {
  126. slides[slides.length - 1].transition = $this.attr('data-animation');
  127. }
  128. }
  129. if ($this.is('video')) {
  130. if ($this.children().length > 0) {
  131. slides.push({
  132. video: {
  133. src: [],
  134. loop: eval($this.attr('data-loop') || false),
  135. mute: eval($this.attr('data-mute') || true),
  136. }
  137. });
  138. var video_srces = [];
  139. $this.children().each(function() {
  140. video_srces.push($(this).attr('src'))
  141. });
  142. slides[slides.length - 1].video.src = video_srces;
  143. } else {
  144. slides.push({
  145. video: {
  146. src: [$this.attr('src')],
  147. loop: eval($this.attr('data-loop') || false),
  148. mute: eval($this.attr('data-mute') || true),
  149. }
  150. });
  151. }
  152. }
  153. });
  154. this.settings.slides = slides;
  155. },
  156. _arrows: function(){
  157. let settings = window.attr_prefix(this.settings, 'arrow');
  158. let this_prev = this.$elmt.find('.vegas-prev');
  159. let this_next = this.$elmt.find('.vegas-next');
  160. if (settings.prevHook != null) {
  161. $("body").on("click", settings.prevHook, function() {
  162. $('[data-arrow-prevHook="' + settings.prevHook + '"] .vegas-prev').click();
  163. });
  164. }
  165. if (settings.nextHook != null) {
  166. $("body").on("click", settings.nextHook, function() {
  167. $('[data-arrow-nextHook="' + settings.nextHook + '"] .vegas-next').click();
  168. });
  169. }
  170. if (settings.status == 'none') {
  171. this_prev.hide();
  172. this_next.hide();
  173. } else if (settings.status == 'hide') {
  174. if (this.slide == 0) {
  175. this.$elmt.find('.vegas-prev').hide();
  176. }
  177. this.$elmt.on('vegaschange', function() {
  178. if ($(this).vegas('current') <= 0) {
  179. $(this).find('.vegas-prev').hide();
  180. } else {
  181. $(this).find('.vegas-prev').show();
  182. }
  183. if ($(this).vegas('current') >= ($(this).vegas('__total') - 1)) {
  184. $(this).find('.vegas-next').hide();
  185. } else {
  186. $(this).find('.vegas-next').show();
  187. }
  188. });
  189. } else if (settings.status == 'disabled') {
  190. if (this.slide == 0) {
  191. this.$elmt.find('.vegas-prev').addClass('vegas-disabled');
  192. }
  193. this.$elmt.on('vegaschange', function() {
  194. if ($(this).vegas('current') <= 0) {
  195. $(this).find('.vegas-prev').addClass('vegas-disabled');
  196. } else {
  197. $(this).find('.vegas-prev').removeClass('vegas-disabled');
  198. }
  199. if ($(this).vegas('current') >= ($(this).vegas('__total') - 1)) {
  200. $(this).find('.vegas-next').addClass('vegas-disabled');
  201. } else {
  202. $(this).find('.vegas-next').removeClass('vegas-disabled');
  203. }
  204. });
  205. }
  206. if (settings.hookStatus == 'none') {
  207. $(settings.prevHook).hide();
  208. $(settings.nextHook).hide();
  209. } else if (settings.hookStatus == 'hide') {
  210. if (this.slide == 0) {
  211. $(settings.prevHook).hide();
  212. }
  213. this.$elmt.on('vegaschange', function() {
  214. if ($(this).vegas('current') <= 0) {
  215. $(settings.prevHook).hide();
  216. } else {
  217. $(settings.prevHook).show();
  218. }
  219. if ($(this).vegas('current') >= ($(this).vegas('__total') - 1)) {
  220. $(settings.nextHook).hide();
  221. } else {
  222. $(settings.nextHook).show();
  223. }
  224. });
  225. } else if (settings.hookStatus == 'disabled') {
  226. if (this.slide == 0) {
  227. $(settings.prevHook).addClass('vegas-disabled');
  228. }
  229. this.$elmt.on('vegaschange', function() {
  230. if ($(this).vegas('current') <= 0) {
  231. $(settings.prevHook).addClass('vegas-disabled');
  232. } else {
  233. $(settings.prevHook).removeClass('vegas-disabled');
  234. }
  235. if ($(this).vegas('current') >= ($(this).vegas('__total') - 1)) {
  236. $(settings.nextHook).addClass('vegas-disabled');
  237. } else {
  238. $(settings.nextHook).removeClass('vegas-disabled');
  239. }
  240. });
  241. }
  242. },
  243. _dots: function() {
  244. let settings = window.attr_prefix(this.settings, 'dot');
  245. let dots = this.$elmt.find('.vegas-dots .vegas-dot').eq(0);
  246. let dataHook = $(settings.hook);
  247. let dataHookChild = $(settings.hook).children().eq(0);
  248. for (var i = 1; i < this.settings.slides.length; i++) {
  249. dots.clone(true).appendTo(this.$elmt.find('.vegas-dots'));
  250. if (settings.hookStatus == "clone") {
  251. dataHookChild.clone(true).appendTo(dataHook);
  252. }
  253. if (settings.hookStatus == "clone-id") {
  254. var vegas_dot = dataHookChild.clone(true);
  255. vegas_dot.html(function(x, oldHTML) {
  256. return oldHTML.replace(/\${i}/g, i + 1);
  257. }).appendTo(dataHook);
  258. }
  259. }
  260. if (settings.hookStatus == "clone-id") {
  261. dataHookChild.html(function(x, oldHTML) {
  262. return oldHTML.replace(/\${i}/g, 1);
  263. })
  264. }
  265. if (settings.status == "hide") {
  266. this.$elmt.find('.vegas-dots').hide();
  267. }
  268. this.$elmt.find('.vegas-dots .vegas-dot').eq(this.slide).addClass('vegas-dot-active');
  269. $(settings.hook).children().eq(this.slide).addClass('vegas-dot-active');
  270. this.$elmt.on('vegaschange', function() {
  271. $(this).find('.vegas-dots .vegas-dot.vegas-dot-active').removeClass('vegas-dot-active');
  272. $(settings.hook).children('.vegas-dot-active').removeClass('vegas-dot-active');
  273. var active_dot = $(this).vegas('current') > $(this).find('.vegas-dots .vegas-dot').length - 1 ? $(this).find('.vegas-dots .vegas-dot').length - 1 : $(this).vegas('current');
  274. var active_dot = $(this).vegas('current') < 0 ? 0 : active_dot;
  275. $(this).find('.vegas-dots .vegas-dot').eq(active_dot).addClass('vegas-dot-active');
  276. $(settings.hook).children().eq(active_dot).addClass('vegas-dot-active');
  277. });
  278. var dot_clone = settings.hook;
  279. $(settings.hook).children().on('click', function() {
  280. $('[data-dot-hook="' + dot_clone + '"] .vegas-dots').children('.vegas-dot').eq($(this).index()).click();
  281. });
  282. },
  283. _init: function() {
  284. var $content, $contentScroll, $overlay, $timer, isBody = this.elmt.tagName === 'BODY', timer = this.settings.timer, overlay = this.settings.overlay, self = this;
  285. // Preloading
  286. this._preload();
  287. // Div with scrollable content
  288. if (!isBody) {
  289. $contentScroll = $('<div class="vegas-content-scrollable">');
  290. $content = $('<div class="vegas-content">').css('overflow', this.$elmt.css('overflow')).css('padding', this.$elmt.css('padding'));
  291. // Some browsers don't compute padding shorthand
  292. if (!this.$elmt.css('padding')) {
  293. $content.css('padding-top', this.$elmt.css('padding-top')).css('padding-bottom', this.$elmt.css('padding-bottom')).css('padding-left', this.$elmt.css('padding-left')).css('padding-right', this.$elmt.css('padding-right'));
  294. }
  295. this.$elmt.css('padding', 0);
  296. this.$elmt.clone(true).children().appendTo($content);
  297. $content.children().not('.vegas-dot-controls').wrapAll("<div class='contents'></div>");
  298. $content.append($('<div class="vegas-arrow-controls"></div>'));
  299. $content.find('.vegas-arrow-controls').append($('<div class="vegas-prev"></div>')).find('.vegas-prev').append($('<div class="vegas-prev-icon"></div>'));
  300. $content.find('.vegas-arrow-controls').append($('<div class="vegas-next"></div>')).find('.vegas-next').append($('<div class="vegas-next-icon"></div>'));
  301. $content.append($('<div class="vegas-dot-controls"></div>')).find('.vegas-dot-controls').append($('<div class="vegas-dots"></div>')).find('.vegas-dots').append($('<div class="vegas-dot"></div>'));
  302. this.elmt.innerHTML = '';
  303. }
  304. // Timer
  305. if (timer && this.support.transition) {
  306. $timer = $('<div class="vegas-timer"><div class="vegas-timer-progress">');
  307. this.$timer = $timer;
  308. this.$elmt.prepend($timer);
  309. }
  310. // Overlay
  311. if (overlay) {
  312. $overlay = $('<div class="vegas-overlay">');
  313. if (typeof overlay === 'string') {
  314. $overlay.css('background-image', 'url(' + overlay + ')');
  315. }
  316. this.$overlay = $overlay;
  317. this.$elmt.prepend($overlay);
  318. }
  319. // Container
  320. this.$elmt.addClass('vegas-container');
  321. if (!isBody) {
  322. this.$elmt.append($contentScroll);
  323. $contentScroll.append($content);
  324. }
  325. this.$elmt.data('vegas', this);
  326. setTimeout(function() {
  327. self.trigger('init');
  328. self._goto(self.slide);
  329. if (self.settings.autoplay) {
  330. self.trigger('play');
  331. }
  332. }, 1);
  333. },
  334. _preload: function() {
  335. var img, i;
  336. for (i = 0; i < this.settings.slides.length; i++) {
  337. if (this.settings.preload || this.settings.preloadImages) {
  338. if (this.settings.slides[i].src) {
  339. img = new Image();
  340. img.src = this.settings.slides[i].src;
  341. }
  342. }
  343. if (this.settings.preload || this.settings.preloadVideos) {
  344. if (this.support.video && this.settings.slides[i].video) {
  345. if (this.settings.slides[i].video instanceof Array) {
  346. this._video(this.settings.slides[i].video);
  347. } else {
  348. this._video(this.settings.slides[i].video.src);
  349. }
  350. }
  351. }
  352. }
  353. },
  354. _random: function(array) {
  355. return array[Math.floor(Math.random() * array.length)];
  356. },
  357. _slideShow: function() {
  358. var self = this;
  359. if (this.total > 1 && !this.ended && !this.paused && !this.noshow) {
  360. this.timeout = setTimeout(function() {
  361. self.next();
  362. }, this._options('delay'));
  363. }
  364. },
  365. _timer: function(state) {
  366. var self = this;
  367. clearTimeout(this.timeout);
  368. if (!this.$timer) {
  369. return;
  370. }
  371. this.$timer.removeClass('vegas-timer-running').find('div').css('transition-duration', '0ms');
  372. if (this.ended || this.paused || this.noshow) {
  373. return;
  374. }
  375. if (state) {
  376. setTimeout(function() {
  377. self.$timer.addClass('vegas-timer-running').find('div').css('transition-duration', self._options('delay') - 100 + 'ms');
  378. }, 100);
  379. }
  380. },
  381. _video: function(srcs) {
  382. var video, source, cacheKey = srcs.toString();
  383. if (videoCache[cacheKey]) {
  384. return videoCache[cacheKey];
  385. }
  386. if (!(srcs instanceof Array)) {
  387. srcs = [srcs];
  388. }
  389. video = document.createElement('video');
  390. video.preload = true;
  391. srcs.forEach(function(src) {
  392. source = document.createElement('source');
  393. source.src = src;
  394. video.appendChild(source);
  395. });
  396. videoCache[cacheKey] = video;
  397. return video;
  398. },
  399. _fadeOutSound: function(video, duration) {
  400. var self = this
  401. , delay = duration / 10
  402. , volume = video.volume - 0.09;
  403. if (volume > 0) {
  404. video.volume = volume;
  405. setTimeout(function() {
  406. self._fadeOutSound(video, duration);
  407. }, delay);
  408. } else {
  409. video.pause();
  410. }
  411. },
  412. _fadeInSound: function(video, duration) {
  413. var self = this
  414. , delay = duration / 10
  415. , volume = video.volume + 0.09;
  416. if (volume < 1) {
  417. video.volume = volume;
  418. setTimeout(function() {
  419. self._fadeInSound(video, duration);
  420. }, delay);
  421. }
  422. },
  423. _options: function(key, i) {
  424. if (i === undefined) {
  425. i = this.slide;
  426. }
  427. if (this.settings.slides[i][key] !== undefined) {
  428. return this.settings.slides[i][key];
  429. }
  430. return this.settings[key];
  431. },
  432. _goto: function(nb) {
  433. if (typeof this.settings.slides[nb] === 'undefined') {
  434. nb = 0;
  435. }
  436. this.slide = nb;
  437. var $slide, $inner, $video, $slides = this.$elmt.children('.vegas-slide'), src = this.settings.slides[nb].src, videoSettings = this.settings.slides[nb].video, delay = this._options('delay'), align = this._options('align'), valign = this._options('valign'), cover = this._options('cover'), color = this._options('color') || this.$elmt.css('background-color'), self = this, total = $slides.length, video, img;
  438. var transition = this._options('transition')
  439. , transitionDuration = this._options('transitionDuration')
  440. , animation = this._options('animation')
  441. , animationDuration = this._options('animationDuration');
  442. if (this.settings.firstTransition && this.first) {
  443. transition = this.settings.firstTransition || transition;
  444. }
  445. if (this.settings.firstTransitionDuration && this.first) {
  446. transitionDuration = this.settings.firstTransitionDuration || transitionDuration;
  447. }
  448. if (this.first) {
  449. this.first = false;
  450. }
  451. if (cover !== 'repeat') {
  452. if (cover === true) {
  453. cover = 'cover';
  454. } else if (cover === false) {
  455. cover = 'contain';
  456. }
  457. }
  458. if (transition === 'random' || transition instanceof Array) {
  459. if (transition instanceof Array) {
  460. transition = this._random(transition);
  461. } else {
  462. transition = this._random(this.transitions);
  463. }
  464. }
  465. if (animation === 'random' || animation instanceof Array) {
  466. if (animation instanceof Array) {
  467. animation = this._random(animation);
  468. } else {
  469. animation = this._random(this.animations);
  470. }
  471. }
  472. if (transitionDuration === 'auto' || transitionDuration > delay) {
  473. transitionDuration = delay;
  474. }
  475. if (animationDuration === 'auto') {
  476. animationDuration = delay;
  477. }
  478. $slide = $('<div class="vegas-slide"></div>');
  479. if (this.support.transition && transition) {
  480. $slide.addClass('vegas-transition-' + transition);
  481. }
  482. if (this.support.video && videoSettings) {
  483. // Video
  484. if (videoSettings instanceof Array) {
  485. video = this._video(videoSettings);
  486. } else {
  487. video = this._video(videoSettings.src);
  488. }
  489. video.loop = videoSettings.loop !== undefined ? videoSettings.loop : true;
  490. video.muted = videoSettings.mute !== undefined ? videoSettings.mute : true;
  491. if (video.muted === false) {
  492. video.volume = 0;
  493. this._fadeInSound(video, transitionDuration);
  494. } else {
  495. video.pause();
  496. }
  497. $video = $(video).addClass('vegas-video').css('background-color', color);
  498. if (this.support.objectFit) {
  499. $video.css('object-position', align + ' ' + valign).css('object-fit', cover).css('width', '100%').css('height', '100%');
  500. } else if (cover === 'contain') {
  501. $video.css('width', '100%').css('height', '100%');
  502. }
  503. $slide.append($video);
  504. } else {
  505. // Image
  506. img = new Image();
  507. $inner = $('<div class="vegas-slide-inner"></div>').css('background-image', 'url("' + src + '")').css('background-color', color).css('background-position', align + ' ' + valign);
  508. if (cover === 'repeat') {
  509. $inner.css('background-repeat', 'repeat');
  510. } else {
  511. $inner.css('background-size', cover);
  512. }
  513. if (this.support.transition && animation) {
  514. $inner.addClass('vegas-animation-' + animation).css('animation-duration', animationDuration + 'ms');
  515. }
  516. $slide.append($inner);
  517. }
  518. if (!this.support.transition) {
  519. $slide.css('display', 'none');
  520. }
  521. if (total) {
  522. $slides.eq(total - 1).after($slide);
  523. } else {
  524. this.$elmt.prepend($slide);
  525. }
  526. $slides.css('transition', 'all 0ms').each(function() {
  527. this.className = 'vegas-slide';
  528. if (this.tagName === 'VIDEO') {
  529. this.className += ' vegas-video';
  530. }
  531. if (transition) {
  532. this.className += ' vegas-transition-' + transition;
  533. this.className += ' vegas-transition-' + transition + '-in';
  534. }
  535. });
  536. self._timer(false);
  537. function go() {
  538. self._timer(true);
  539. setTimeout(function() {
  540. if (transition) {
  541. if (self.support.transition) {
  542. $slides.css('transition', 'all ' + transitionDuration + 'ms').addClass('vegas-transition-' + transition + '-out').css('opacity', self.settings.prevTransparent ? 0 : 1);
  543. $slides.each(function() {
  544. var video = $slides.find('video').get(0);
  545. if (video) {
  546. video.volume = 1;
  547. self._fadeOutSound(video, transitionDuration);
  548. }
  549. });
  550. $slide.css('transition', 'all ' + transitionDuration + 'ms').addClass('vegas-transition-' + transition + '-in');
  551. } else {
  552. $slide.fadeIn(transitionDuration);
  553. }
  554. }
  555. for (var i = 0; i < $slides.length - self.settings.slidesToKeep; i++) {
  556. $slides.eq(i).remove();
  557. }
  558. self.trigger('walk');
  559. self._slideShow();
  560. }, 100);
  561. }
  562. if (video) {
  563. if (video.readyState === 4) {
  564. video.currentTime = 0;
  565. }
  566. video.play();
  567. go();
  568. } else {
  569. img.src = src;
  570. if (img.complete) {
  571. go();
  572. } else {
  573. img.onload = go;
  574. }
  575. }
  576. },
  577. _end: function() {
  578. this.ended = !this.settings.autoplay;
  579. this._timer(false);
  580. this.trigger('end');
  581. },
  582. shuffle: function() {
  583. var temp, rand;
  584. for (var i = this.total - 1; i > 0; i--) {
  585. rand = Math.floor(Math.random() * (i + 1));
  586. temp = this.settings.slides[i];
  587. this.settings.slides[i] = this.settings.slides[rand];
  588. this.settings.slides[rand] = temp;
  589. }
  590. },
  591. play: function() {
  592. if (this.paused) {
  593. this.paused = false;
  594. this.next();
  595. this.trigger('play');
  596. }
  597. },
  598. pause: function() {
  599. this._timer(false);
  600. this.paused = true;
  601. this.trigger('pause');
  602. },
  603. toggle: function() {
  604. if (this.paused) {
  605. this.play();
  606. } else {
  607. this.pause();
  608. }
  609. },
  610. playing: function() {
  611. return !this.paused && !this.noshow;
  612. },
  613. current: function(advanced) {
  614. if (advanced) {
  615. return {
  616. slide: this.slide,
  617. data: this.settings.slides[this.slide]
  618. };
  619. }
  620. return this.slide;
  621. },
  622. jump: function(nb) {
  623. if (nb < 0 || nb > this.total - 1 || nb === this.slide) {
  624. return;
  625. }
  626. this.trigger('jump');
  627. this.slide = nb;
  628. this.trigger('change');
  629. this._goto(this.slide);
  630. },
  631. next: function() {
  632. this.slide++;
  633. this.trigger('next');
  634. this.trigger('change');
  635. if (this.slide >= this.total) {
  636. this.slide--;
  637. if (!this.settings.loop) {
  638. return this._end();
  639. }
  640. this.slide = 0;
  641. }
  642. this._goto(this.slide);
  643. },
  644. previous: function() {
  645. this.slide--;
  646. this.trigger('previous');
  647. this.trigger('change');
  648. if (this.slide < 0) {
  649. if (!this.settings.loop) {
  650. this.slide++;
  651. return;
  652. } else {
  653. this.slide = this.total - 1;
  654. }
  655. }
  656. this._goto(this.slide);
  657. },
  658. trigger: function(fn) {
  659. var params = [];
  660. if (fn === 'init') {
  661. params = [this.settings];
  662. } else {
  663. params = [this.slide, this.settings.slides[this.slide]];
  664. }
  665. this.$elmt.trigger('vegas' + fn, params);
  666. if (typeof this.settings[fn] === 'function') {
  667. this.settings[fn].apply(this.$elmt, params);
  668. }
  669. },
  670. options: function(key, value) {
  671. var oldSlides = this.settings.slides.slice();
  672. if (typeof key === 'object') {
  673. this.settings = $.extend({}, defaults, $.vegas.defaults, key);
  674. } else if (typeof key === 'string') {
  675. if (value === undefined) {
  676. return this.settings[key];
  677. }
  678. this.settings[key] = value;
  679. } else {
  680. return this.settings;
  681. }
  682. // In case slides have changed
  683. if (this.settings.slides !== oldSlides) {
  684. this.total = this.settings.slides.length;
  685. this.noshow = this.total < 2;
  686. this._preload();
  687. }
  688. },
  689. destroy: function() {
  690. clearTimeout(this.timeout);
  691. this.$elmt.removeClass('vegas-container');
  692. this.$elmt.find('> .vegas-slide').remove();
  693. this.$elmt.find('> .vegas-wrapper').clone(true).children().appendTo(this.$elmt);
  694. this.$elmt.find('> .vegas-wrapper').remove();
  695. if (this.settings.timer) {
  696. this.$timer.remove();
  697. }
  698. if (this.settings.overlay) {
  699. this.$overlay.remove();
  700. }
  701. this.elmt._vegas = null;
  702. }
  703. };
  704. $.fn.vegas = function(options) {
  705. var args = arguments, error = false, returns;
  706. if (options === undefined || typeof options === 'object') {
  707. return this.each(function() {
  708. if (!this._vegas) {
  709. this._vegas = new Vegas(this,options);
  710. }
  711. });
  712. } else if (typeof options === 'string') {
  713. this.each(function() {
  714. var instance = this._vegas;
  715. if (!instance) {
  716. throw new Error('No Vegas applied to this element.');
  717. }
  718. if (options[0] === '_' && options[1] === '_'){
  719. returns = instance[options.substring(2)];
  720. } else {
  721. if (typeof instance[options] === 'function' && options[0] !== '_') {
  722. returns = instance[options].apply(instance, [].slice.call(args, 1));
  723. } else {
  724. error = true;
  725. }
  726. }
  727. });
  728. if (error) {
  729. throw new Error('No method "' + options + '" in Vegas.');
  730. }
  731. return returns !== undefined ? returns : this;
  732. }
  733. }
  734. ;
  735. $.vegas = {};
  736. $.vegas.defaults = defaults;
  737. $.vegas.isVideoCompatible = function() {
  738. return !/(Android|webOS|Phone|iPad|iPod|BlackBerry|Windows Phone)/i.test(navigator.userAgent);
  739. }
  740. ;
  741. }
  742. )(window.jQuery || window.Zepto || window.m4q);
  743. $(document).ready(function() {
  744. if (typeof $.initialize == 'function'){
  745. $.initialize('[type=vegas]', function(event) {
  746. if (! window.delegate_vegas){
  747. window.delegate_vegas = true;
  748. delegate_function();
  749. }
  750. if ($(this).is(':not("[vegas]")')){
  751. $(this).vegas().attr('vegas', '');
  752. }
  753. });
  754. } else {
  755. window.init_vegas = function(){
  756. $('[type="vegas"]:not("[vegas]")').vegas().attr('vegas', '');
  757. }
  758. if ($('[type="vegas"]:not("[vegas]")').length > 0){
  759. window.init_vegas();
  760. window.delegate_vegas = true;
  761. delegate_function();
  762. }
  763. }
  764. });
  765. function delegate_function(){
  766. $("body").delegate("[type=vegas][vegas]", "mousedown", function() {
  767. var e = window.event;
  768. startX = e.clientX;
  769. window.sliding = true;
  770. });
  771. $("body").delegate("[type=vegas][vegas]", "mouseup", function() {
  772. var e = window.event;
  773. var endX = e.clientX;
  774. // 左滑
  775. if ((startX - endX) > 30) {
  776. $(this).vegas('next');
  777. }
  778. // 右滑
  779. if ((endX - startX) > 30) {
  780. $(this).vegas('previous');
  781. }
  782. window.sliding = false;
  783. });
  784. $("body").delegate("[type=vegas][vegas]", "mouseleave", function() {
  785. if (window.sliding) {
  786. var e = window.event;
  787. var endX = e.clientX;
  788. // 左滑
  789. if ((startX - endX) > 30) {
  790. $(this).vegas('next');
  791. }
  792. // 右滑
  793. if ((endX - startX) > 30) {
  794. $(this).vegas('previous');
  795. }
  796. window.sliding = false;
  797. }
  798. });
  799. $("body").delegate('[type=vegas][vegas] .vegas-prev', "click", function() {
  800. $(this).parents('[type=vegas]').vegas('previous');
  801. });
  802. $("body").delegate('[type=vegas][vegas] .vegas-next', "click", function() {
  803. $(this).parents('[type=vegas]').vegas('next');
  804. });
  805. $("body").delegate('[type=vegas][vegas] .vegas-dots .vegas-dot', "click", function() {
  806. $(this).parents('[type=vegas]').vegas('jump', $(this).index());
  807. });
  808. }