UNPKG

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