Skip to content
This repository was archived by the owner on Feb 24, 2023. It is now read-only.

Commit 3aba356

Browse files
author
Łukasz Florczak
committed
Autoplay mode and responsive settings – optymalization
1 parent ad1baa0 commit 3aba356

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

src/Agile.vue

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
slides: null
114114
},
115115
slidesCount: 0,
116+
autoplayStatus: false,
116117
autoplayTimeout: null,
117118
allSlidesCount: 0,
118119
currentSlide: 0,
@@ -164,15 +165,6 @@
164165
165166
// Set first load settings
166167
Object.assign(this.settings, this.defaultSettings)
167-
168-
// Protection against contradictory settings
169-
if (this.settings.autoplay) {
170-
this.settings.infinite = true
171-
}
172-
173-
if (this.settings.pauseOnDotsHover) {
174-
this.settings.dots = true
175-
}
176168
},
177169
178170
mounted () {
@@ -307,36 +299,47 @@
307299
},
308300
309301
enableAutoplayMode () {
310-
// Autoplay
302+
// Protection against contradictory settings
311303
if (this.settings.autoplay) {
312-
if (this.settings.pauseOnHover) {
313-
this.el.track.addEventListener('mouseover', this.stopAutoplay)
314-
this.el.track.addEventListener('mouseout', this.startAutoplay)
315-
}
304+
this.settings.infinite = true
305+
}
316306
317-
if (this.settings.pauseOnDotsHover) {
318-
for (let i = 0; i < this.slidesCount; ++i) {
319-
this.el.dots[i].addEventListener('mouseover', this.stopAutoplay)
320-
this.el.dots[i].addEventListener('mouseout', this.startAutoplay)
321-
}
307+
if (this.settings.pauseOnDotsHover) {
308+
this.settings.dots = true
309+
}
310+
311+
// Autoplay
312+
if (this.settings.pauseOnHover) {
313+
this.el.track.addEventListener('mouseover', this.stopAutoplay)
314+
this.el.track.addEventListener('mouseout', this.startAutoplay)
315+
}
316+
317+
if (this.settings.pauseOnDotsHover) {
318+
for (let i = 0; i < this.slidesCount; ++i) {
319+
this.el.dots[i].addEventListener('mouseover', this.stopAutoplay)
320+
this.el.dots[i].addEventListener('mouseout', this.startAutoplay)
322321
}
323322
}
323+
324+
this.autoplayStatus = true
325+
this.startAutoplay()
324326
},
325327
326328
disableAutoplayMode () {
327-
if (this.settings.autoplay) {
328-
if (this.settings.pauseOnHover) {
329-
this.el.track.removeEventListener('mouseover', this.stopAutoplay)
330-
this.el.track.removeEventListener('mouseout', this.startAutoplay)
331-
}
329+
if (this.settings.pauseOnHover) {
330+
this.el.track.removeEventListener('mouseover', this.stopAutoplay)
331+
this.el.track.removeEventListener('mouseout', this.startAutoplay)
332+
}
332333
333-
if (this.settings.pauseOnDotsHover) {
334-
for (let i = 0; i < this.slidesCount; ++i) {
335-
this.el.dots[i].removeEventListener('mouseover', this.stopAutoplay)
336-
this.el.dots[i].removeEventListener('mouseout', this.startAutoplay)
337-
}
334+
if (this.settings.pauseOnDotsHover) {
335+
for (let i = 0; i < this.slidesCount; ++i) {
336+
this.el.dots[i].removeEventListener('mouseover', this.stopAutoplay)
337+
this.el.dots[i].removeEventListener('mouseout', this.startAutoplay)
338338
}
339339
}
340+
341+
this.autoplayStatus = false
342+
this.stopAutoplay()
340343
},
341344
342345
countSlides () {
@@ -365,6 +368,13 @@
365368
366369
startAutoplay () {
367370
this.autoplayTimeout = setTimeout(() => {
371+
if (!this.settings.autoplay) {
372+
this.stopAutoplay()
373+
this.disableAutoplayMode()
374+
this.autoplayStatus = false
375+
return false
376+
}
377+
368378
this.nextSlide()
369379
}, this.autoplaySpeed)
370380
},
@@ -490,11 +500,11 @@
490500
}
491501
492502
// Check autoplay mode status and enable/disable
493-
if (this.settings.autoplay) {
503+
if (this.settings.autoplay && !this.autoplayStatus) {
494504
this.enableAutoplayMode()
495-
this.startAutoplay()
496-
} else {
497-
this.stopAutoplay()
505+
}
506+
507+
if (!this.settings.autoplay && this.autoplayStatus) {
498508
this.disableAutoplayMode()
499509
}
500510

0 commit comments

Comments
 (0)