Skip to content

Commit 3830eb2

Browse files
committed
introduce delayed tap on mobile for prev/next navigation
1 parent 3950a4f commit 3830eb2

File tree

1 file changed

+47
-30
lines changed

1 file changed

+47
-30
lines changed

src/js/index.js

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,13 +1005,15 @@ export default function Tobii (userOptions) {
10051005
DRAG.y = event.pageY
10061006

10071007
if (!isZoomed()) {
1008-
// Evaluate drag animation
1008+
// Drag animation
10091009
const deltaX = DRAG.startX - DRAG.x
10101010
const deltaY = DRAG.startY - DRAG.y
10111011

10121012
// Skip animation if drag distance is too low
10131013
if (distance(deltaX, deltaY) < 10) return
10141014

1015+
groups[activeGroup].slider.classList.add('tobii__slider--is-dragging')
1016+
10151017
if (Math.abs(deltaX) > Math.abs(deltaY) && groups[activeGroup].elementsLength > 1) {
10161018
// Horizontal swipe
10171019
groups[activeGroup].slider.style.transform =
@@ -1022,8 +1024,6 @@ export default function Tobii (userOptions) {
10221024
`translate(${offset}px, -${Math.round(deltaY)}px)`
10231025
}
10241026
}
1025-
1026-
groups[activeGroup].slider.classList.add('tobii__slider--is-dragging')
10271027
}
10281028

10291029
/**
@@ -1033,46 +1033,63 @@ export default function Tobii (userOptions) {
10331033
const pointerupHandler = (event) => {
10341034
event.stopPropagation()
10351035

1036+
// Remove this event from the target's cache
1037+
const index = pointerDownCache.findIndex(
1038+
(cachedEv) => cachedEv.pointerId === event.pointerId
1039+
)
1040+
pointerDownCache.splice(index, 1)
1041+
10361042
const isDragging = DRAG.startX !== DRAG.x || DRAG.startY !== DRAG.y
1043+
if (!isDragging) {
1044+
// Evaluate tap
1045+
const currentTime = new Date().getTime()
1046+
const tapLength = currentTime - lastTapTime
1047+
if (tapLength < DOUBLE_TAP_TIME && tapLength > 100) {
1048+
// Double click
1049+
event.preventDefault()
1050+
lastTapTime = 0
1051+
if (isZoomed()) {
1052+
resetZoom()
1053+
} else {
1054+
zoomPan(MAX_SCALE / 2, event.clientX, event.clientY, 0, 0)
1055+
}
1056+
} else {
1057+
lastTapTime = currentTime
1058+
if (isTouchDevice()) {
1059+
// Delayed tap on mobile
1060+
window.setTimeout(() => {
1061+
const { left, top, bottom, right, width } = event.target.getBoundingClientRect()
1062+
if (DRAG.startY < top || DRAG.startY > bottom || !lastTapTime) return
1063+
if (DRAG.startX > left && DRAG.startX < left + width / 2) {
1064+
previous()
1065+
} else if (DRAG.startX < right && DRAG.startX > right - width / 2) {
1066+
next()
1067+
}
1068+
}, DOUBLE_TAP_TIME)
1069+
}
1070+
}
1071+
}
1072+
10371073
if (isDragging && !isZoomed()) {
1038-
const MOVEMENT_X = DRAG.x - DRAG.startX
1039-
const MOVEMENT_Y = DRAG.y - DRAG.startY
1074+
// Evaluate drag
1075+
groups[activeGroup].slider.classList.remove('tobii__slider--is-dragging')
1076+
1077+
const MOVEMENT_X = DRAG.startX - DRAG.x
1078+
const MOVEMENT_Y = DRAG.startY - DRAG.y
10401079
const MOVEMENT_X_DISTANCE = Math.abs(MOVEMENT_X)
10411080
const MOVEMENT_Y_DISTANCE = Math.abs(MOVEMENT_Y)
10421081

1043-
if (MOVEMENT_X > 0 && MOVEMENT_X_DISTANCE > userSettings.threshold && groups[activeGroup].currentIndex > 0) {
1082+
if (MOVEMENT_X < 0 && MOVEMENT_X_DISTANCE > userSettings.threshold && groups[activeGroup].currentIndex > 0) {
10441083
previous()
1045-
} else if (MOVEMENT_X < 0 && MOVEMENT_X_DISTANCE > userSettings.threshold &&
1084+
} else if (MOVEMENT_X > 0 && MOVEMENT_X_DISTANCE > userSettings.threshold &&
10461085
groups[activeGroup].currentIndex !== groups[activeGroup].elementsLength - 1) {
10471086
next()
1048-
} else if (MOVEMENT_Y < 0 && MOVEMENT_Y_DISTANCE > userSettings.threshold && userSettings.swipeClose) {
1087+
} else if (MOVEMENT_Y > 0 && MOVEMENT_Y_DISTANCE > userSettings.threshold && userSettings.swipeClose) {
10491088
close()
10501089
} else {
10511090
updateOffset()
10521091
}
10531092
}
1054-
1055-
const currentTime = new Date().getTime()
1056-
const tapLength = currentTime - lastTapTime
1057-
if (tapLength < DOUBLE_TAP_TIME && tapLength > 100) {
1058-
event.preventDefault()
1059-
if (isZoomed()) {
1060-
resetZoom()
1061-
} else {
1062-
zoomPan(MAX_SCALE / 2, event.clientX, event.clientY, 0, 0)
1063-
}
1064-
lastTapTime = 0
1065-
} else {
1066-
lastTapTime = currentTime
1067-
}
1068-
1069-
// Remove this event from the target's cache
1070-
const index = pointerDownCache.findIndex(
1071-
(cachedEv) => cachedEv.pointerId === event.pointerId
1072-
)
1073-
pointerDownCache.splice(index, 1)
1074-
1075-
groups[activeGroup].slider.classList.remove('tobii__slider--is-dragging')
10761093
}
10771094

10781095
/**

0 commit comments

Comments
 (0)