feat(ImagePreview): add swipe when image is moved to edge (#11505)

This commit is contained in:
neverland 2023-01-23 09:35:08 +08:00 committed by GitHub
parent 7a42c9a616
commit 46b23e4bad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 8 deletions

View File

@ -144,6 +144,7 @@ export default defineComponent({
let startDistance: number; let startDistance: number;
let doubleTapTimer: ReturnType<typeof setTimeout> | null; let doubleTapTimer: ReturnType<typeof setTimeout> | null;
let touchStartTime: number; let touchStartTime: number;
let isImageMoved = false;
const onTouchStart = (event: TouchEvent) => { const onTouchStart = (event: TouchEvent) => {
const { touches } = event; const { touches } = event;
@ -161,6 +162,9 @@ export default defineComponent({
startMoveY = state.moveY; startMoveY = state.moveY;
touchStartTime = Date.now(); touchStartTime = Date.now();
// whether the image position is moved after scaling
isImageMoved = false;
state.moving = fingerNum === 1 && state.scale !== 1; state.moving = fingerNum === 1 && state.scale !== 1;
state.zooming = fingerNum === 2 && !offsetX.value; state.zooming = fingerNum === 2 && !offsetX.value;
@ -175,23 +179,36 @@ export default defineComponent({
touch.move(event); touch.move(event);
if (state.moving || state.zooming) {
preventDefault(event, true);
}
if (state.moving) { if (state.moving) {
const { deltaX, deltaY } = touch; const { deltaX, deltaY } = touch;
const moveX = deltaX.value + startMoveX; const moveX = deltaX.value + startMoveX;
const moveY = deltaY.value + startMoveY; const moveY = deltaY.value + startMoveY;
// if the image is moved to the edge, no longer trigger move,
// allow user to swipe to next image
if (
(moveX > maxMoveX.value || moveX < -maxMoveX.value) &&
!isImageMoved
) {
state.moving = false;
return;
}
isImageMoved = true;
preventDefault(event, true);
state.moveX = clamp(moveX, -maxMoveX.value, maxMoveX.value); state.moveX = clamp(moveX, -maxMoveX.value, maxMoveX.value);
state.moveY = clamp(moveY, -maxMoveY.value, maxMoveY.value); state.moveY = clamp(moveY, -maxMoveY.value, maxMoveY.value);
} }
if (state.zooming && touches.length === 2) { if (state.zooming) {
const distance = getDistance(touches); preventDefault(event, true);
const scale = (startScale * distance) / startDistance;
setScale(scale); if (touches.length === 2) {
const distance = getDistance(touches);
const scale = (startScale * distance) / startDistance;
setScale(scale);
}
} }
}; };

View File

@ -24,6 +24,7 @@ export default {
defaultLang: 'en-US', defaultLang: 'en-US',
darkModeClass: 'van-theme-dark', darkModeClass: 'van-theme-dark',
lightModeClass: 'van-theme-light', lightModeClass: 'van-theme-light',
enableVConsole: false,
versions: [ versions: [
{ label: 'v1', link: '/vant/v1/' }, { label: 'v1', link: '/vant/v1/' },
{ label: 'v2', link: '/vant/v2/' }, { label: 'v2', link: '/vant/v2/' },