|
@@ -0,0 +1,237 @@
|
|
|
|
|
+<script setup lang="ts">
|
|
|
|
|
+import { ref, watch, onMounted, onUnmounted } from 'vue'
|
|
|
|
|
+
|
|
|
|
|
+const props = defineProps<{
|
|
|
|
|
+ show: boolean
|
|
|
|
|
+ src: string
|
|
|
|
|
+}>()
|
|
|
|
|
+
|
|
|
|
|
+const emit = defineEmits<{
|
|
|
|
|
+ (e: 'update:show', value: boolean): void
|
|
|
|
|
+}>()
|
|
|
|
|
+
|
|
|
|
|
+const scale = ref(1)
|
|
|
|
|
+const translateX = ref(0)
|
|
|
|
|
+const translateY = ref(0)
|
|
|
|
|
+const isDragging = ref(false)
|
|
|
|
|
+const startX = ref(0)
|
|
|
|
|
+const startY = ref(0)
|
|
|
|
|
+
|
|
|
|
|
+// 重置状态
|
|
|
|
|
+const resetTransform = () => {
|
|
|
|
|
+ scale.value = 1
|
|
|
|
|
+ translateX.value = 0
|
|
|
|
|
+ translateY.value = 0
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// 监听打开时重置状态
|
|
|
|
|
+watch(
|
|
|
|
|
+ () => props.show,
|
|
|
|
|
+ (newVal) => {
|
|
|
|
|
+ if (newVal) {
|
|
|
|
|
+ resetTransform()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+const close = () => {
|
|
|
|
|
+ emit('update:show', false)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const zoomIn = () => {
|
|
|
|
|
+ scale.value = Math.min(scale.value + 0.25, 5) // 最大放大 5 倍
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const zoomOut = () => {
|
|
|
|
|
+ scale.value = Math.max(scale.value - 0.25, 0.5) // 最小缩小到 0.5 倍
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// 滚轮缩放
|
|
|
|
|
+const handleWheel = (e: WheelEvent) => {
|
|
|
|
|
+ if (e.deltaY < 0) zoomIn()
|
|
|
|
|
+ else zoomOut()
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// 拖拽平移
|
|
|
|
|
+const handleMouseDown = (e: MouseEvent) => {
|
|
|
|
|
+ if (scale.value <= 1) return // 没放大时不支持拖拽
|
|
|
|
|
+ isDragging.value = true
|
|
|
|
|
+ startX.value = e.clientX - translateX.value
|
|
|
|
|
+ startY.value = e.clientY - translateY.value
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const handleMouseMove = (e: MouseEvent) => {
|
|
|
|
|
+ if (!isDragging.value) return
|
|
|
|
|
+ translateX.value = e.clientX - startX.value
|
|
|
|
|
+ translateY.value = e.clientY - startY.value
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const handleMouseUp = () => {
|
|
|
|
|
+ isDragging.value = false
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// 键盘 Esc 关闭
|
|
|
|
|
+const handleKeyDown = (e: KeyboardEvent) => {
|
|
|
|
|
+ if (e.key === 'Escape' && props.show) close()
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+onMounted(() => window.addEventListener('keydown', handleKeyDown))
|
|
|
|
|
+onUnmounted(() => window.removeEventListener('keydown', handleKeyDown))
|
|
|
|
|
+</script>
|
|
|
|
|
+
|
|
|
|
|
+<template>
|
|
|
|
|
+ <Teleport to="body">
|
|
|
|
|
+ <div
|
|
|
|
|
+ v-if="show"
|
|
|
|
|
+ class="custom-lightbox"
|
|
|
|
|
+ @wheel.prevent="handleWheel"
|
|
|
|
|
+ @mousedown="handleMouseDown"
|
|
|
|
|
+ @mousemove="handleMouseMove"
|
|
|
|
|
+ @mouseup="handleMouseUp"
|
|
|
|
|
+ @mouseleave="handleMouseUp"
|
|
|
|
|
+ >
|
|
|
|
|
+ <!-- 遮罩层 -->
|
|
|
|
|
+ <div class="lightbox-backdrop" @click="close"></div>
|
|
|
|
|
+
|
|
|
|
|
+ <!-- 图片容器 -->
|
|
|
|
|
+ <div
|
|
|
|
|
+ class="lightbox-img-wrapper"
|
|
|
|
|
+ :style="{
|
|
|
|
|
+ transform: `translate(${translateX}px, ${translateY}px) scale(${scale})`,
|
|
|
|
|
+ cursor: scale > 1 ? (isDragging ? 'grabbing' : 'grab') : 'default'
|
|
|
|
|
+ }"
|
|
|
|
|
+ >
|
|
|
|
|
+ <img :src="src" alt="preview" @mousedown.stop @click.stop />
|
|
|
|
|
+ </div>
|
|
|
|
|
+
|
|
|
|
|
+ <!-- 自定义工具栏 -->
|
|
|
|
|
+ <div class="custom-toolbar" @mousedown.stop @click.stop>
|
|
|
|
|
+ <!-- 缩小 -->
|
|
|
|
|
+ <button class="tool-btn" title="缩小" @click="zoomOut">
|
|
|
|
|
+ <svg
|
|
|
|
|
+ viewBox="0 0 24 24"
|
|
|
|
|
+ width="20"
|
|
|
|
|
+ height="20"
|
|
|
|
|
+ stroke="currentColor"
|
|
|
|
|
+ stroke-width="2"
|
|
|
|
|
+ fill="none"
|
|
|
|
|
+ stroke-linecap="round"
|
|
|
|
|
+ stroke-linejoin="round"
|
|
|
|
|
+ >
|
|
|
|
|
+ <circle cx="11" cy="11" r="8"></circle>
|
|
|
|
|
+ <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
|
|
|
|
|
+ <line x1="8" y1="11" x2="14" y2="11"></line>
|
|
|
|
|
+ </svg>
|
|
|
|
|
+ </button>
|
|
|
|
|
+
|
|
|
|
|
+ <!-- 放大 -->
|
|
|
|
|
+ <button class="tool-btn" title="放大" @click="zoomIn">
|
|
|
|
|
+ <svg
|
|
|
|
|
+ viewBox="0 0 24 24"
|
|
|
|
|
+ width="20"
|
|
|
|
|
+ height="20"
|
|
|
|
|
+ stroke="currentColor"
|
|
|
|
|
+ stroke-width="2"
|
|
|
|
|
+ fill="none"
|
|
|
|
|
+ stroke-linecap="round"
|
|
|
|
|
+ stroke-linejoin="round"
|
|
|
|
|
+ >
|
|
|
|
|
+ <circle cx="11" cy="11" r="8"></circle>
|
|
|
|
|
+ <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
|
|
|
|
|
+ <line x1="11" y1="8" x2="11" y2="14"></line>
|
|
|
|
|
+ <line x1="8" y1="11" x2="14" y2="11"></line>
|
|
|
|
|
+ </svg>
|
|
|
|
|
+ </button>
|
|
|
|
|
+
|
|
|
|
|
+ <!-- 关闭 -->
|
|
|
|
|
+ <button class="tool-btn" title="关闭" @click="close">
|
|
|
|
|
+ <svg
|
|
|
|
|
+ viewBox="0 0 24 24"
|
|
|
|
|
+ width="20"
|
|
|
|
|
+ height="20"
|
|
|
|
|
+ stroke="currentColor"
|
|
|
|
|
+ stroke-width="2"
|
|
|
|
|
+ fill="none"
|
|
|
|
|
+ stroke-linecap="round"
|
|
|
|
|
+ stroke-linejoin="round"
|
|
|
|
|
+ >
|
|
|
|
|
+ <line x1="18" y1="6" x2="6" y2="18"></line>
|
|
|
|
|
+ <line x1="6" y1="6" x2="18" y2="18"></line>
|
|
|
|
|
+ </svg>
|
|
|
|
|
+ </button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </Teleport>
|
|
|
|
|
+</template>
|
|
|
|
|
+
|
|
|
|
|
+<style scoped>
|
|
|
|
|
+.custom-lightbox {
|
|
|
|
|
+ position: fixed;
|
|
|
|
|
+ inset: 0;
|
|
|
|
|
+ z-index: 9999;
|
|
|
|
|
+ display: flex;
|
|
|
|
|
+ align-items: center;
|
|
|
|
|
+ justify-content: center;
|
|
|
|
|
+ overflow: hidden;
|
|
|
|
|
+ user-select: none;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+.lightbox-backdrop {
|
|
|
|
|
+ position: absolute;
|
|
|
|
|
+ inset: 0;
|
|
|
|
|
+ background: rgba(0, 0, 0, 0.85);
|
|
|
|
|
+ backdrop-filter: blur(4px);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+.lightbox-img-wrapper {
|
|
|
|
|
+ position: relative;
|
|
|
|
|
+ z-index: 1;
|
|
|
|
|
+ transition: transform 0.1s ease-out;
|
|
|
|
|
+ display: flex;
|
|
|
|
|
+ align-items: center;
|
|
|
|
|
+ justify-content: center;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+.lightbox-img-wrapper img {
|
|
|
|
|
+ max-width: 90vw;
|
|
|
|
|
+ max-height: 90vh;
|
|
|
|
|
+ object-fit: contain;
|
|
|
|
|
+ pointer-events: none; /* 防止图片自带的拖拽影响 */
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+.custom-toolbar {
|
|
|
|
|
+ position: absolute;
|
|
|
|
|
+ bottom: 40px;
|
|
|
|
|
+ z-index: 2;
|
|
|
|
|
+ display: flex;
|
|
|
|
|
+ gap: 12px;
|
|
|
|
|
+ background: rgba(30, 30, 30, 0.7);
|
|
|
|
|
+ border: 1px solid rgba(255, 255, 255, 0.1);
|
|
|
|
|
+ border-radius: 40px;
|
|
|
|
|
+ backdrop-filter: blur(8px);
|
|
|
|
|
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+.tool-btn {
|
|
|
|
|
+ display: flex;
|
|
|
|
|
+ align-items: center;
|
|
|
|
|
+ justify-content: center;
|
|
|
|
|
+ width: 36px;
|
|
|
|
|
+ height: 36px;
|
|
|
|
|
+ border-radius: 50%;
|
|
|
|
|
+ background: transparent;
|
|
|
|
|
+ border: none;
|
|
|
|
|
+ color: #e5e5e5;
|
|
|
|
|
+ cursor: pointer;
|
|
|
|
|
+ transition: all 0.2s;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+.tool-btn:hover {
|
|
|
|
|
+ color: #ffffff;
|
|
|
|
|
+ transform: scale(1.1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+.tool-btn:active {
|
|
|
|
|
+ transform: scale(0.95);
|
|
|
|
|
+}
|
|
|
|
|
+</style>
|