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