vue3 手撸图片查看

71 阅读3分钟
<script setup lang="ts">
const props = defineProps({
  images: {
    type: Array,
    required: true,
    default: () => [],
  },
  initialIndex: {
    type: Number,
    default: 0,
  },
});

const emit = defineEmits(['image-changed']);

const currentIndex = ref(props.initialIndex);
const scale = ref(1);
const rotation = ref(0);
const isFullscreen = ref(false);
const imageRef = ref(null);
const isDragging = ref(false);
const startX = ref(0);
const startY = ref(0);
const translateX = ref(0);
const translateY = ref(0);

const currentImage = computed(() => props.images[currentIndex.value]);

const imageStyle = computed(() => ({
  transform: `scale(${scale.value}) rotate(${rotation.value}deg) translate(${translateX.value}px, ${translateY.value}px)`,
  cursor: scale.value > 1 ? 'move' : 'pointer',
}));

// 图片切换
const switchImage = (index: number) => {
  currentIndex.value = index;
  resetTransform();
  emit('image-changed', index);
};

const prevImage = () => {
  if (currentIndex.value > 0) {
    switchImage(currentIndex.value - 1);
  }
};

const nextImage = () => {
  if (currentIndex.value < props.images.length - 1) {
    switchImage(currentIndex.value + 1);
  }
};

// 缩放功能
const zoomIn = () => {
  scale.value = Math.min(scale.value + 0.2, 5);
};

const zoomOut = () => {
  scale.value = Math.max(scale.value - 0.2, 0.2);
};

const resetZoom = () => {
  scale.value = 1;
  translateX.value = 0;
  translateY.value = 0;
};

// 旋转功能
const rotateImage = () => {
  rotation.value = (rotation.value + 90) % 360;
};

// 重置所有变换
const resetTransform = () => {
  scale.value = 1;
  rotation.value = 0;
  translateX.value = 0;
  translateY.value = 0;
};

// 鼠标滚轮缩放
const handleWheel = (e: any) => {
  e.preventDefault();
  if (e.deltaY < 0) {
    zoomIn();
  } else {
    zoomOut();
  }
};

// 全屏功能
const toggleFullscreen = () => {
  isFullscreen.value = !isFullscreen.value;
};

// 拖拽功能
const startDrag = (e: any) => {
  if (scale.value <= 1) return;

  isDragging.value = true;
  startX.value = e.clientX - translateX.value;
  startY.value = e.clientY - translateY.value;

  document.addEventListener('mousemove', handleDrag);
  document.addEventListener('mouseup', stopDrag);
};

const handleDrag = (e: any) => {
  if (!isDragging.value) return;

  translateX.value = e.clientX - startX.value;
  translateY.value = e.clientY - startY.value;
};

const stopDrag = () => {
  isDragging.value = false;
  document.removeEventListener('mousemove', handleDrag);
  document.removeEventListener('mouseup', stopDrag);
};

// 键盘快捷键
const handleKeyDown = (e: any) => {
  if (isFullscreen.value) {
    switch (e.key) {
      case 'ArrowLeft':
        prevImage();
        break;
      case 'ArrowRight':
        nextImage();
        break;
      case '+':
      case '=':
        zoomIn();
        break;
      case '-':
        zoomOut();
        break;
      case '0':
        resetZoom();
        break;
      case 'r':
        rotateImage();
        break;
      case 'Escape':
        toggleFullscreen();
        break;
    }
  }
};

onMounted(() => {
  window.addEventListener('keydown', handleKeyDown);
});

onUnmounted(() => {
  window.removeEventListener('keydown', handleKeyDown);
});
</script>

<template>
  <div class="image-viewer-container">
    <!-- 图片显示区域 -->
    <div
      class="image-display-area"
      @wheel.prevent="handleWheel"
    >
      <img
        ref="imageRef"
        :src="currentImage"
        :style="imageStyle"
        alt="当前图片"
        @click="toggleFullscreen"
        @mousedown="startDrag"
      />
    </div>

    <!-- 控制工具栏 -->
    <div class="toolbar">
      <button
        @click="zoomIn"
        title="放大"
      >
        <svg
          viewBox="0 0 24 24"
          width="20"
          height="20"
        >
          <path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z" />
        </svg>
      </button>
      <button
        @click="zoomOut"
        title="缩小"
      >
        <svg
          viewBox="0 0 24 24"
          width="20"
          height="20"
        >
          <path d="M19 13H5v-2h14v2z" />
        </svg>
      </button>
      <button
        @click="resetZoom"
        title="重置大小"
      >
        <svg
          viewBox="0 0 24 24"
          width="20"
          height="20"
        >
          <path
            d="M12 5V1L7 6l5 5V7c3.31 0 6 2.69 6 6s-2.69 6-6 6-6-2.69-6-6H4c0 4.42 3.58 8 8 8s8-3.58 8-8-3.58-8-8-8z"
          />
        </svg>
      </button>
      <button
        @click="rotateImage"
        title="旋转"
      >
        <svg
          viewBox="0 0 24 24"
          width="20"
          height="20"
        >
          <path
            d="M12 6v3l4-4-4-4v3c-4.42 0-8 3.58-8 8 0 1.57.46 3.03 1.24 4.26L6.7 14.8c-.45-.83-.7-1.79-.7-2.8 0-3.31 2.69-6 6-6zm6.76 1.74L17.3 9.2c.44.84.7 1.79.7 2.8 0 3.31-2.69 6-6 6v-3l-4 4 4 4v-3c4.42 0 8-3.58 8-8 0-1.57-.46-3.03-1.24-4.26z"
          />
        </svg>
      </button>
      <button
        @click="prevImage"
        :disabled="currentIndex === 0"
        title="上一张"
      >
        <svg
          viewBox="0 0 24 24"
          width="20"
          height="20"
        >
          <path d="M15.41 16.59L10.83 12l4.58-4.59L14 6l-6 6 6 6 1.41-1.41z" />
        </svg>
      </button>
      <span class="image-counter">{{ currentIndex + 1 }} / {{ images.length }}</span>
      <button
        @click="nextImage"
        :disabled="currentIndex === images.length - 1"
        title="下一张"
      >
        <svg
          viewBox="0 0 24 24"
          width="20"
          height="20"
        >
          <path d="M8.59 16.59L13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.41z" />
        </svg>
      </button>
    </div>

    <!-- 缩略图列表 -->
    <div
      class="thumbnail-container"
      v-if="images.length > 1"
    >
      <div
        v-for="(img, index) in images"
        :key="index"
        class="thumbnail"
        :class="{ active: index === currentIndex }"
        @click="switchImage(index)"
      >
        <img
          :src="img"
          alt="缩略图"
        />
      </div>
    </div>

    <!-- 全屏模式 -->
    <div
      v-if="isFullscreen"
      class="fullscreen-overlay"
      @click="toggleFullscreen"
    >
      <div
        class="fullscreen-content"
        @click.stop
      >
        <img
          :src="currentImage"
          :style="imageStyle"
          alt="全屏图片"
          @mousedown="startDrag"
        />
        <button
          class="close-btn"
          @click="toggleFullscreen"
        >
          <svg
            viewBox="0 0 24 24"
            width="24"
            height="24"
          >
            <path
              d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"
            />
          </svg>
        </button>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.image-viewer-container {
  position: relative;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  overflow: hidden;
  background-color: #f5f5f5;
}

.image-display-area {
  width: 100%;
  height: 400px;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  background-color: #eee;
  cursor: pointer;
}

.image-display-area img {
  max-width: 100%;
  max-height: 100%;
  transition: transform 0.2s ease;
  user-select: none;
}

.toolbar {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px;
  background-color: #fff;
  border-top: 1px solid #e0e0e0;
  gap: 10px;
}

.toolbar button {
  background: none;
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  color: #555;
  transition: all 0.2s;
}

.toolbar button:hover {
  background-color: #f0f0f0;
  color: #000;
}

.toolbar button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.toolbar button svg {
  fill: currentColor;
}

.image-counter {
  margin: 0 10px;
  font-size: 14px;
  color: #666;
}

.thumbnail-container {
  display: flex;
  padding: 10px;
  gap: 10px;
  overflow-x: auto;
  background-color: #fff;
  border-top: 1px solid #e0e0e0;
}

.thumbnail {
  width: 60px;
  height: 60px;
  border: 2px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  cursor: pointer;
  flex-shrink: 0;
  opacity: 0.7;
  transition: all 0.2s;
}

.thumbnail:hover {
  opacity: 1;
}

.thumbnail.active {
  border-color: #2196f3;
  opacity: 1;
}

.thumbnail img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.fullscreen-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.9);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.fullscreen-content {
  position: relative;
  max-width: 90%;
  max-height: 90%;
}

.fullscreen-content img {
  max-width: 100%;
  max-height: 90vh;
  object-fit: contain;
}

.close-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0, 0, 0, 0.5);
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  color: white;
}

.close-btn svg {
  fill: currentColor;
}
</style>