移动端JS图片预览怎么支持双指缩放和滑动关闭

作者:窗边听雨时间:2026-12-06浏览:537

使用 CSS transform 配合 JavaScript 触摸事件( touchstart , touchmove , touchend )实现双指缩放与单指滑动关闭。核心逻辑是计算两指间距变化量更新图片 scale ,计算中心点位移判断是否触发关闭。

移动端JS图片预览怎么支持双指缩放和滑动关闭

触摸事件与手势识别

移动端浏览器默认会拦截部分触摸行为以处理页面滚动和缩放,因此需要在容器上设置 touch-action: none 并监听原生触摸事件。通过 e.touches.length 判断当前手指数量,区分单指拖动、双指缩放和平移操作。

移动端JS图片预览怎么支持双指缩放和滑动关闭

单指操作时,记录起始坐标与当前坐标的差值,用于实现拖拽位移;双指操作时,利用 Math.hypot 计算两指距离,将当前距离与上一帧距离对比,得出缩放比例因子。为避免误触,可设定最小移动阈值过滤抖动。

完整交互程序实现

以下代码创建一个全屏遮罩层,点击缩略图后显示大图。支持双指捏合缩放、单指拖动平移,以及向上快速滑动超过阈值自动关闭。

移动端JS图片预览怎么支持双指缩放和滑动关闭

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>JS 图片预览</title>
<style>
  body { margin: 0; font-family: sans-serif; }
  .thumb { width: 100px; height: 100px; object-fit: cover; cursor: pointer; }
  .overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.9); display: none; justify-content: center;
    align-items: center; z-index: 1000; touch-action: none;
  }
  .preview-img {
    max-width: 90%; max-height: 90%; transition: transform 0.1s ease-out;
    transform-origin: center center;
  }
  .overlay.closing .preview-img {
    transition: transform 0.3s ease-in, opacity 0.3s;
    transform: scale(0.5) translateY(-100vh) !important;
    opacity: 0;
  }
</style>
</head>
<body>
<img src="https://via.placeholder.com/600x400" class="thumb" id="thumb">
<div class="overlay" id="overlay">
  <img src="https://via.placeholder.com/600x400" class="preview-img" id="preview">
</div>
<script>
const thumb = document.getElementById('thumb');
const overlay = document.getElementById('overlay');
const preview = document.getElementById('preview');
let state = { scale: 1, x: 0, y: 0, startX: 0, startY: 0, distStart: 0, isMoving: false };

thumb.addEventListener('click', () => {
  overlay.style.display = 'flex';
  resetTransform();
});

overlay.addEventListener('touchstart', e => {
  if (e.touches.length === 2) {
    const t1 = e.touches[0], t2 = e.touches[1];
    state.distStart = Math.hypot(t2.clientX - t1.clientX, t2.clientY - t1.clientY);
  } else if (e.touches.length === 1) {
    state.startX = e.touches[0].clientX;
    state.startY = e.touches[0].clientY;
    state.isMoving = true;
  }
});

overlay.addEventListener('touchmove', e => {
  e.preventDefault();
  if (e.touches.length === 2) {
    const t1 = e.touches[0], t2 = e.touches[1];
    const dist = Math.hypot(t2.clientX - t1.clientX, t2.clientY - t1.clientY);
    const newScale = Math.max(0.5, Math.min(3, state.scale * (dist / state.distStart)));
    applyTransform(newScale, state.x, state.y);
    state.scale = newScale;
    state.distStart = dist;
  } else if (e.touches.length === 1 && state.isMoving) {
    const dx = e.touches[0].clientX - state.startX;
    const dy = e.touches[0].clientY - state.startY;
    applyTransform(state.scale, state.x + dx, state.y + dy);
  }
});

overlay.addEventListener('touchend', e => {
  if (e.touches.length === 0 && state.isMoving) {
    // Simple swipe up detection logic could be enhanced with velocity
    if (state.y < -window.innerHeight * 0.3) closePreview();
    state.isMoving = false;
  }
});

function applyTransform(s, tx, ty) {
  preview.style.transform = `translate(${tx}px, ${ty}px) scale(${s})`;
}

function resetTransform() {
  state = { scale: 1, x: 0, y: 0, startX: 0, startY: 0, distStart: 0, isMoving: false };
  overlay.classList.remove('closing');
  applyTransform(1, 0, 0);
}

function closePreview() {
  overlay.classList.add('closing');
  setTimeout(() => {
    overlay.style.display = 'none';
    resetTransform();
  }, 300);
}
</script>
</body>
</html>

运行后点击图片,遮罩层出现。双指捏合可放大缩小图片,单指拖动可平移。向上滑动超过屏幕高度 30% 时触发关闭动画,0.3秒后完全隐藏并重置状态。代码中使用了 touch-action: none 防止页面滚动干扰,Math.hypot 计算两点距离实现精准缩放。

将代码保存为 image-preview.html,在本地服务器或手机浏览器中打开。点击页面上的图片即可体验预览功能。向上滑动速度足够快或距离超过屏幕高度的 30% 时,图片会平滑退出并隐藏。

关闭动画与状态重置

关闭操作需结合 CSS transition 实现平滑过渡,并在动画结束后彻底重置 DOM 状态,防止残留样式影响后续交互。

当检测到关闭条件满足时,先移除缩放和平移类名,添加退出动画类。监听 transitionend 事件,在动画完成后将图片透明度设为 0,恢复初始位置,并移除遮罩层可见性。这样确保下次打开时界面干净,且不会因之前的变换矩阵导致布局错乱。

作者最新文章
相关文章
更多