Move jumping to real page to separate fn

This commit is contained in:
Vadim
2021-07-15 19:16:23 +03:00
parent 211e390286
commit fa7020ed6b
2 changed files with 20 additions and 3 deletions

View File

@@ -183,15 +183,25 @@
} }
function offsetPage(animated) { function offsetPage(animated) {
// _duration is an offset animation time
_duration = animated ? duration : 0 _duration = animated ? duration : 0
offset = -currentPageIndex * pageWidth offset = -currentPageIndex * pageWidth
}
// makes delayed jump to 1st or last element
function jumpIfNeeded() {
let jumped = false
if (infinite) { if (infinite) {
if (currentPageIndex === 0) { if (currentPageIndex === 0) {
showPage(pagesCount - 2, { offsetDelayMs: duration, animated: false }) // offsetDelayMs should depend on _duration, as it wait when offset finishes
showPage(pagesCount - 2, { offsetDelayMs: _duration, animated: false })
jumped = true
} else if (currentPageIndex === pagesCount - 1) { } else if (currentPageIndex === pagesCount - 1) {
showPage(1, { offsetDelayMs: duration, animated: false }) showPage(1, { offsetDelayMs: _duration, animated: false })
jumped = true
} }
} }
return jumped
} }
// Disable page change while animation is in progress // Disable page change while animation is in progress
@@ -211,8 +221,11 @@
const offsetDelayMs = get(options, 'offsetDelayMs', true) const offsetDelayMs = get(options, 'offsetDelayMs', true)
safeChangePage(() => { safeChangePage(() => {
store.moveToPage({ pageIndex, pagesCount }) store.moveToPage({ pageIndex, pagesCount })
// delayed page transition, used for infinite autoplay to jump to real page
setTimeout(() => { setTimeout(() => {
offsetPage(animated) offsetPage(animated)
const jumped = jumpIfNeeded()
!jumped && applyAutoplay()
}, offsetDelayMs) }, offsetDelayMs)
}, { animated }) }, { animated })
} }
@@ -221,6 +234,8 @@
safeChangePage(() => { safeChangePage(() => {
store.prev({ infinite, pagesCount }) store.prev({ infinite, pagesCount })
offsetPage(animated) offsetPage(animated)
const jumped = jumpIfNeeded()
!jumped && applyAutoplay()
}, { animated }) }, { animated })
} }
function showNextPage(options) { function showNextPage(options) {
@@ -228,6 +243,8 @@
safeChangePage(() => { safeChangePage(() => {
store.next({ infinite, pagesCount }) store.next({ infinite, pagesCount })
offsetPage(animated) offsetPage(animated)
const jumped = jumpIfNeeded()
!jumped && applyAutoplay()
}, { animated }) }, { animated })
} }

View File

@@ -43,7 +43,7 @@
} }
.sc-carousel-dots__dot-container { .sc-carousel-dots__dot-container {
height: calc(var(--sc-dot-size) + 10px); height: calc(var(--sc-dot-size) + 10px);
width: calc(var(--sc-dot-size) + 10px); width: calc(var(--sc-dot-size) + 6px);
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;