autoplay.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import {
  2. nextTick
  3. } from '../../shared/utils.js';
  4. export default function Autoplay({
  5. swiper,
  6. extendParams,
  7. on,
  8. emit
  9. }) {
  10. let timeout;
  11. swiper.autoplay = {
  12. running: false,
  13. paused: false
  14. };
  15. extendParams({
  16. autoplay: {
  17. enabled: false,
  18. delay: 3000,
  19. waitForTransition: true,
  20. disableOnInteraction: true,
  21. stopOnLastSlide: false,
  22. reverseDirection: false,
  23. pauseOnMouseEnter: false
  24. }
  25. });
  26. function run() {
  27. const $activeSlideEl = swiper.slides[swiper.activeIndex];
  28. let delay = swiper.params.autoplay.delay;
  29. clearTimeout(timeout);
  30. timeout = nextTick(() => {
  31. let autoplayResult;
  32. if (swiper.params.autoplay.reverseDirection) {
  33. if (swiper.params.loop) {
  34. swiper.loopFix();
  35. autoplayResult = swiper.slidePrev(swiper.params.speed, true, true);
  36. emit('autoplay');
  37. } else if (!swiper.isBeginning) {
  38. autoplayResult = swiper.slidePrev(swiper.params.speed, true, true);
  39. emit('autoplay');
  40. } else if (!swiper.params.autoplay.stopOnLastSlide) {
  41. autoplayResult = swiper.slideTo(swiper.slides.length - 1, swiper.params.speed, true, true);
  42. emit('autoplay');
  43. } else {
  44. stop();
  45. }
  46. } else if (swiper.params.loop) {
  47. swiper.loopFix();
  48. setTimeout(() => {
  49. autoplayResult = swiper.slideNext(swiper.params.speed, true, true);
  50. }, 30)
  51. emit('autoplay');
  52. } else if (!swiper.isEnd) {
  53. autoplayResult = swiper.slideNext(swiper.params.speed, true, true);
  54. emit('autoplay');
  55. } else if (!swiper.params.autoplay.stopOnLastSlide) {
  56. autoplayResult = swiper.slideTo(0, swiper.params.speed, true, true);
  57. emit('autoplay');
  58. } else {
  59. stop();
  60. }
  61. if (swiper.params.cssMode && swiper.autoplay.running) run();
  62. else if (autoplayResult === false) {
  63. run();
  64. }
  65. }, delay);
  66. }
  67. function start() {
  68. if (typeof timeout !== 'undefined') return false;
  69. if (swiper.autoplay.running) return false;
  70. swiper.autoplay.running = true;
  71. emit('autoplayStart');
  72. run();
  73. return true;
  74. }
  75. function stop() {
  76. if (!swiper.autoplay.running) return false;
  77. if (typeof timeout === 'undefined') return false;
  78. if (timeout) {
  79. clearTimeout(timeout);
  80. timeout = undefined;
  81. }
  82. swiper.autoplay.running = false;
  83. emit('autoplayStop');
  84. return true;
  85. }
  86. function pause(speed) {
  87. if (!swiper.autoplay.running) return;
  88. if (swiper.autoplay.paused) return;
  89. if (timeout) clearTimeout(timeout);
  90. swiper.autoplay.paused = true;
  91. if (speed === 0 || !swiper.params.autoplay.waitForTransition) {
  92. swiper.autoplay.paused = false;
  93. run();
  94. } else {
  95. ['transitionEnd', 'webkitTransitionEnd'].forEach(event => {
  96. swiper.on(event, onTransitionEnd);
  97. });
  98. }
  99. }
  100. function onVisibilityChange() {
  101. // const document = getDocument();
  102. // if (document.visibilityState === 'hidden' && swiper.autoplay.running) {
  103. // pause();
  104. // }
  105. // if (document.visibilityState === 'visible' && swiper.autoplay.paused) {
  106. // run();
  107. // swiper.autoplay.paused = false;
  108. // }
  109. }
  110. function onTransitionEnd(e) {
  111. if (!swiper || swiper.destroyed || !swiper.$wrapperEl) return;
  112. // if (e.target !== swiper.$wrapperEl[0]) return;
  113. ['transitionEnd', 'webkitTransitionEnd'].forEach(event => {
  114. swiper.off(event, onTransitionEnd);
  115. });
  116. swiper.autoplay.paused = false;
  117. if (!swiper.autoplay.running) {
  118. stop();
  119. } else {
  120. run();
  121. }
  122. }
  123. function onMouseEnter() {
  124. if (swiper.params.autoplay.disableOnInteraction) {
  125. stop();
  126. } else {
  127. pause();
  128. }
  129. // ['transitionend', 'webkitTransitionEnd'].forEach(event => {
  130. // swiper.$wrapperEl[0].removeEventListener(event, onTransitionEnd);
  131. // });
  132. }
  133. function onMouseLeave() {
  134. if (swiper.params.autoplay.disableOnInteraction) {
  135. return;
  136. }
  137. swiper.autoplay.paused = false;
  138. run();
  139. }
  140. function attachMouseEvents() {
  141. if (swiper.params.autoplay.pauseOnMouseEnter) {}
  142. }
  143. function detachMouseEvents() {}
  144. on('init update', () => {
  145. if (swiper.params.autoplay.enabled) {
  146. start();
  147. attachMouseEvents();
  148. }
  149. });
  150. on('beforeTransitionStart', (_s, speed, internal) => {
  151. if (swiper.autoplay.running) {
  152. if (internal || !swiper.params.autoplay.disableOnInteraction) {
  153. swiper.autoplay.pause(speed);
  154. } else {
  155. if (!swiper.params.loop) {
  156. stop();
  157. }
  158. }
  159. }
  160. });
  161. on('sliderFirstMove', () => {
  162. if (swiper.autoplay.running) {
  163. if (swiper.params.autoplay.disableOnInteraction) {
  164. stop();
  165. } else {
  166. pause();
  167. }
  168. }
  169. });
  170. on('touch-end', () => {
  171. if (swiper.params.cssMode && swiper.autoplay.paused && !swiper.params.autoplay.disableOnInteraction) {
  172. run();
  173. }
  174. });
  175. on('destroy', () => {
  176. detachMouseEvents();
  177. if (swiper.autoplay.running) {
  178. stop();
  179. }
  180. });
  181. Object.assign(swiper.autoplay, {
  182. pause,
  183. run,
  184. start,
  185. stop
  186. });
  187. }