optimize comparison container sizing logic and reduce CSS duplication

This commit is contained in:
jingrow 2025-11-19 21:52:34 +08:00
parent 92208b0513
commit 2a47e6a705

View File

@ -170,7 +170,7 @@
</template>
<script setup lang="ts">
import { ref, computed, nextTick, watch, onMounted, onUnmounted } from 'vue'
import { ref, computed, watch, onMounted, onUnmounted } from 'vue'
import { useMessage } from 'naive-ui'
import axios from 'axios'
import { t } from '@/shared/i18n'
@ -220,55 +220,43 @@ const isDraggingSplitLine = ref(false)
//
const adjustContainerSize = () => {
nextTick(() => {
if (!comparisonContainerRef.value) return
const container = comparisonContainerRef.value
if (!container) return
// 使
const img = originalImageRef.value || resultImageRef.value
if (!img) return
const img = originalImageRef.value || resultImageRef.value
if (!img) return
// preview-section
const container = comparisonContainerRef.value
const previewSection = container.closest('.preview-section') as HTMLElement
if (!previewSection) return
// preview-section
const previewSection = container.closest('.preview-section') as HTMLElement
if (!previewSection) return
const previewRect = previewSection.getBoundingClientRect()
// padding (12px * 2 = 24px)
const maxAvailableWidth = Math.max(0, previewRect.width - 24)
const maxAvailableHeight = Math.max(0, previewRect.height - 24)
const previewRect = previewSection.getBoundingClientRect()
// padding (12px * 2 = 24px)
const padding = 24
const maxAvailableWidth = Math.max(0, previewRect.width - padding)
const maxAvailableHeight = Math.max(0, previewRect.height - padding)
if (maxAvailableWidth <= 0 || maxAvailableHeight <= 0) return
if (maxAvailableWidth <= 0 || maxAvailableHeight <= 0) return
//
if (!img.complete) {
img.addEventListener('load', adjustContainerSize, { once: true })
return
}
//
if (!img.complete) {
//
// 使 once: true
img.addEventListener('load', adjustContainerSize, { once: true })
return
}
const { naturalWidth, naturalHeight } = img
if (naturalWidth === 0 || naturalHeight === 0) return
const naturalWidth = img.naturalWidth
const naturalHeight = img.naturalHeight
//
const scale = Math.min(
maxAvailableWidth / naturalWidth,
maxAvailableHeight / naturalHeight,
1 //
)
if (naturalWidth === 0 || naturalHeight === 0) return
//
//
const scaleX = maxAvailableWidth / naturalWidth
const scaleY = maxAvailableHeight / naturalHeight
const scale = Math.min(scaleX, scaleY, 1) // 1
const displayWidth = naturalWidth * scale
const displayHeight = naturalHeight * scale
//
const finalWidth = Math.min(displayWidth, maxAvailableWidth)
const finalHeight = Math.min(displayHeight, maxAvailableHeight)
//
comparisonContainerRef.value.style.width = `${finalWidth}px`
comparisonContainerRef.value.style.height = `${finalHeight}px`
})
//
container.style.width = `${naturalWidth * scale}px`
container.style.height = `${naturalHeight * scale}px`
}
//
@ -973,7 +961,7 @@ h1 {
justify-content: center;
overflow: hidden;
min-height: 0;
padding: 12px;
padding: 12px; /* 用于计算可用空间 */
}
@ -1008,8 +996,6 @@ h1 {
.comparison-view {
width: 100%;
height: 100%;
max-width: 100%;
max-height: 100%;
display: flex;
justify-content: center;
align-items: center;
@ -1035,6 +1021,7 @@ h1 {
background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
}
/* 所有对比图片容器的公共样式 */
.comparison-image {
position: absolute;
top: 0;
@ -1047,7 +1034,6 @@ h1 {
img {
display: block;
/* 图片限制在容器内,不超过父元素 */
max-width: 100%;
max-height: 100%;
width: auto;
@ -1057,44 +1043,13 @@ h1 {
}
.original-image {
/* 和 result-image 一样使用 absolute确保填充 comparison-container */
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
/* clip-path 通过内联样式动态设置,用于裁剪显示区域 */
img {
display: block;
/* 图片限制在容器内,不超过父元素 */
max-width: 100%;
max-height: 100%;
width: auto;
height: auto;
object-fit: contain;
}
}
.result-image {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 2;
/* clip-path 通过内联样式动态设置,用于裁剪显示区域 */
img {
display: block;
/* 图片限制在容器内,不超过父元素 */
max-width: 100%;
max-height: 100%;
width: auto;
height: auto;
object-fit: contain;
}
}
/* 拖动竖线 */