mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(ImagePreview): add swipe when image is moved to edge (#11505)
This commit is contained in:
parent
7a42c9a616
commit
46b23e4bad
@ -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,24 +179,37 @@ 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) {
|
||||||
|
preventDefault(event, true);
|
||||||
|
|
||||||
|
if (touches.length === 2) {
|
||||||
const distance = getDistance(touches);
|
const distance = getDistance(touches);
|
||||||
const scale = (startScale * distance) / startDistance;
|
const scale = (startScale * distance) / startDistance;
|
||||||
|
|
||||||
setScale(scale);
|
setScale(scale);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const checkTap = () => {
|
const checkTap = () => {
|
||||||
|
@ -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/' },
|
||||||
|
Loading…
x
Reference in New Issue
Block a user