lg-autoplay.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*! lightgallery - v1.2.21 - 2016-06-28
  2. * http://sachinchoolur.github.io/lightGallery/
  3. * Copyright (c) 2016 Sachin N; Licensed Apache 2.0 */
  4. /**
  5. * Autoplay Plugin
  6. * @version 1.2.0
  7. * @author Sachin N - @sachinchoolur
  8. * @license MIT License (MIT)
  9. */
  10. (function($, window, document, undefined) {
  11. 'use strict';
  12. var defaults = {
  13. autoplay: false,
  14. pause: 5000,
  15. progressBar: true,
  16. fourceAutoplay: false,
  17. autoplayControls: true,
  18. appendAutoplayControlsTo: '.lg-toolbar'
  19. };
  20. /**
  21. * Creates the autoplay plugin.
  22. * @param {object} element - lightGallery element
  23. */
  24. var Autoplay = function(element) {
  25. this.core = $(element).data('lightGallery');
  26. this.$el = $(element);
  27. // Execute only if items are above 1
  28. if (this.core.$items.length < 2) {
  29. return false;
  30. }
  31. this.core.s = $.extend({}, defaults, this.core.s);
  32. this.interval = false;
  33. // Identify if slide happened from autoplay
  34. this.fromAuto = true;
  35. // Identify if autoplay canceled from touch/drag
  36. this.canceledOnTouch = false;
  37. // save fourceautoplay value
  38. this.fourceAutoplayTemp = this.core.s.fourceAutoplay;
  39. // do not allow progress bar if browser does not support css3 transitions
  40. if (!this.core.doCss()) {
  41. this.core.s.progressBar = false;
  42. }
  43. this.init();
  44. return this;
  45. };
  46. Autoplay.prototype.init = function() {
  47. var _this = this;
  48. // append autoplay controls
  49. if (_this.core.s.autoplayControls) {
  50. _this.controls();
  51. }
  52. // Create progress bar
  53. if (_this.core.s.progressBar) {
  54. _this.core.$outer.find('.lg').append('<div class="lg-progress-bar"><div class="lg-progress"></div></div>');
  55. }
  56. // set progress
  57. _this.progress();
  58. // Start autoplay
  59. if (_this.core.s.autoplay) {
  60. _this.startlAuto();
  61. }
  62. // cancel interval on touchstart and dragstart
  63. _this.$el.on('onDragstart.lg.tm touchstart.lg.tm', function() {
  64. if (_this.interval) {
  65. _this.cancelAuto();
  66. _this.canceledOnTouch = true;
  67. }
  68. });
  69. // restore autoplay if autoplay canceled from touchstart / dragstart
  70. _this.$el.on('onDragend.lg.tm touchend.lg.tm onSlideClick.lg.tm', function() {
  71. if (!_this.interval && _this.canceledOnTouch) {
  72. _this.startlAuto();
  73. _this.canceledOnTouch = false;
  74. }
  75. });
  76. };
  77. Autoplay.prototype.progress = function() {
  78. var _this = this;
  79. var _$progressBar;
  80. var _$progress;
  81. _this.$el.on('onBeforeSlide.lg.tm', function() {
  82. // start progress bar animation
  83. if (_this.core.s.progressBar && _this.fromAuto) {
  84. _$progressBar = _this.core.$outer.find('.lg-progress-bar');
  85. _$progress = _this.core.$outer.find('.lg-progress');
  86. if (_this.interval) {
  87. _$progress.removeAttr('style');
  88. _$progressBar.removeClass('lg-start');
  89. setTimeout(function() {
  90. _$progress.css('transition', 'width ' + (_this.core.s.speed + _this.core.s.pause) + 'ms ease 0s');
  91. _$progressBar.addClass('lg-start');
  92. }, 20);
  93. }
  94. }
  95. // Remove setinterval if slide is triggered manually and fourceautoplay is false
  96. if (!_this.fromAuto && !_this.core.s.fourceAutoplay) {
  97. _this.cancelAuto();
  98. }
  99. _this.fromAuto = false;
  100. });
  101. };
  102. // Manage autoplay via play/stop buttons
  103. Autoplay.prototype.controls = function() {
  104. var _this = this;
  105. var _html = '<span class="lg-autoplay-button lg-icon"></span>';
  106. // Append autoplay controls
  107. $(this.core.s.appendAutoplayControlsTo).append(_html);
  108. _this.core.$outer.find('.lg-autoplay-button').on('click.lg', function() {
  109. if ($(_this.core.$outer).hasClass('lg-show-autoplay')) {
  110. _this.cancelAuto();
  111. _this.core.s.fourceAutoplay = false;
  112. } else {
  113. if (!_this.interval) {
  114. _this.startlAuto();
  115. _this.core.s.fourceAutoplay = _this.fourceAutoplayTemp;
  116. }
  117. }
  118. });
  119. };
  120. // Autostart gallery
  121. Autoplay.prototype.startlAuto = function() {
  122. var _this = this;
  123. _this.core.$outer.find('.lg-progress').css('transition', 'width ' + (_this.core.s.speed + _this.core.s.pause) + 'ms ease 0s');
  124. _this.core.$outer.addClass('lg-show-autoplay');
  125. _this.core.$outer.find('.lg-progress-bar').addClass('lg-start');
  126. _this.interval = setInterval(function() {
  127. if (_this.core.index + 1 < _this.core.$items.length) {
  128. _this.core.index++;
  129. } else {
  130. _this.core.index = 0;
  131. }
  132. _this.fromAuto = true;
  133. _this.core.slide(_this.core.index, false, false);
  134. }, _this.core.s.speed + _this.core.s.pause);
  135. };
  136. // cancel Autostart
  137. Autoplay.prototype.cancelAuto = function() {
  138. clearInterval(this.interval);
  139. this.interval = false;
  140. this.core.$outer.find('.lg-progress').removeAttr('style');
  141. this.core.$outer.removeClass('lg-show-autoplay');
  142. this.core.$outer.find('.lg-progress-bar').removeClass('lg-start');
  143. };
  144. Autoplay.prototype.destroy = function() {
  145. this.cancelAuto();
  146. this.core.$outer.find('.lg-progress-bar').remove();
  147. };
  148. $.fn.lightGallery.modules.autoplay = Autoplay;
  149. })(jQuery, window, document);