Commit 84d21769 authored by liujiangnan's avatar liujiangnan

feat: 调整位置

parent bf4638d0
...@@ -277,12 +277,27 @@ body { ...@@ -277,12 +277,27 @@ body {
position: absolute; position: absolute;
width: 600px; width: 600px;
max-height: 400px; max-height: 400px;
min-height: 200px;
background-color: white; background-color: white;
border-radius: 8px; border-radius: 8px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3); box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
border: 1px solid #e0e0e0; border: 1px solid #e0e0e0;
z-index: 1000; z-index: 1000;
overflow: hidden; overflow: hidden;
/* 确保气泡框不会被裁剪 */
max-width: calc(100vw - 40px);
max-height: calc(100vh - 40px);
}
/* 响应式设计 */
@media (max-width: 768px) {
.translate-bubble {
width: calc(100vw - 40px);
max-height: calc(100vh - 80px);
left: 20px !important;
right: 20px !important;
top: 40px !important;
}
} }
.bubble-header { .bubble-header {
......
...@@ -104,11 +104,15 @@ function initializeApp(data) { ...@@ -104,11 +104,15 @@ function initializeApp(data) {
// 监听窗口大小变化,重新计算高亮位置 // 监听窗口大小变化,重新计算高亮位置
window.addEventListener('resize', this.onWindowResize); window.addEventListener('resize', this.onWindowResize);
// 监听窗口大小变化,调整气泡框位置
window.addEventListener('resize', this.adjustBubblePosition);
}, },
beforeDestroy() { beforeDestroy() {
this.cleanup(); this.cleanup();
window.removeEventListener('resize', this.onWindowResize); window.removeEventListener('resize', this.onWindowResize);
window.removeEventListener('resize', this.adjustBubblePosition);
}, },
methods: { methods: {
...@@ -855,25 +859,123 @@ function initializeApp(data) { ...@@ -855,25 +859,123 @@ function initializeApp(data) {
const wordWidth = parseFloat(wordStyle.width); const wordWidth = parseFloat(wordStyle.width);
const wordHeight = parseFloat(wordStyle.height); const wordHeight = parseFloat(wordStyle.height);
// 气泡框位置(在单词右侧) // 获取屏幕尺寸
const bubbleLeft = wordLeft + wordWidth + 10; const screenWidth = window.innerWidth;
const bubbleTop = wordTop - 20; const screenHeight = window.innerHeight;
// 动态计算气泡框尺寸
let bubbleWidth = 600;
let bubbleHeight = 400;
// 如果屏幕太小,调整气泡框尺寸
if (screenWidth < 800) {
bubbleWidth = Math.min(600, screenWidth - 40);
bubbleHeight = Math.min(400, screenHeight - 80);
}
// 尝试多个位置策略
let bubbleLeft, bubbleTop;
// 确保气泡框不超出容器边界 // 策略1:优先在单词右侧
const maxLeft = containerRect.width - 600; // 气泡框宽度约600px bubbleLeft = wordLeft + wordWidth + 10;
const maxTop = containerRect.height - 400; // 气泡框高度约400px bubbleTop = wordTop - 20;
// 如果右侧空间不够,尝试左侧
if (bubbleLeft + bubbleWidth > containerRect.width - 20) {
bubbleLeft = wordLeft - bubbleWidth - 10;
}
// 如果左侧也不够,尝试居中
if (bubbleLeft < 20) {
bubbleLeft = Math.max(20, (containerRect.width - bubbleWidth) / 2);
}
const finalLeft = Math.min(bubbleLeft, maxLeft); // 垂直位置调整 - 考虑动态高度
const finalTop = Math.max(bubbleTop, 10); const maxAvailableHeight = containerRect.height - 40; // 留出边距
const preferredTop = wordTop - 20;
// 如果顶部空间不够,尝试底部
if (preferredTop < 20) {
bubbleTop = wordTop + wordHeight + 10;
} else {
bubbleTop = preferredTop;
}
// 如果底部空间也不够,调整到可见区域
if (bubbleTop + bubbleHeight > containerRect.height - 20) {
bubbleTop = Math.max(20, containerRect.height - bubbleHeight - 20);
}
// 最终边界检查
bubbleLeft = Math.max(20, Math.min(bubbleLeft, containerRect.width - bubbleWidth - 20));
bubbleTop = Math.max(20, Math.min(bubbleTop, containerRect.height - bubbleHeight - 20));
// 调试信息
console.log('气泡框位置计算:', {
wordLeft, wordTop, wordWidth, wordHeight,
bubbleLeft, bubbleTop, bubbleWidth, bubbleHeight,
containerWidth: containerRect.width, containerHeight: containerRect.height,
screenWidth, screenHeight,
maxAvailableHeight
});
return { return {
position: 'absolute', position: 'absolute',
left: finalLeft + 'px', left: bubbleLeft + 'px',
top: finalTop + 'px', top: bubbleTop + 'px',
zIndex: 1000 zIndex: 1000
}; };
}, },
// 动态调整气泡框位置
adjustBubblePosition() {
if (!this.translateBubble.show) return;
const bubbleElement = document.querySelector('.translate-bubble');
if (!bubbleElement) return;
const bubbleRect = bubbleElement.getBoundingClientRect();
const containerRect = bubbleElement.parentElement.getBoundingClientRect();
// 检查是否需要调整位置
let needsAdjustment = false;
let newTop = parseFloat(this.translateBubble.style.top);
// 检查底部是否超出
if (bubbleRect.bottom > containerRect.height - 20) {
newTop = containerRect.height - bubbleRect.height - 20;
needsAdjustment = true;
}
// 检查顶部是否超出
if (bubbleRect.top < 20) {
newTop = 20;
needsAdjustment = true;
}
// 检查右侧是否超出
if (bubbleRect.right > containerRect.width - 20) {
const newLeft = containerRect.width - bubbleRect.width - 20;
this.translateBubble.style.left = newLeft + 'px';
}
// 检查左侧是否超出
if (bubbleRect.left < 20) {
this.translateBubble.style.left = '20px';
}
// 应用垂直位置调整
if (needsAdjustment) {
this.translateBubble.style.top = newTop + 'px';
console.log('动态调整气泡框位置:', {
originalTop: parseFloat(this.translateBubble.style.top),
newTop: newTop,
bubbleHeight: bubbleRect.height,
containerHeight: containerRect.height
});
}
},
// 关闭翻译气泡框 // 关闭翻译气泡框
closeTranslateBubble() { closeTranslateBubble() {
this.translateBubble = { this.translateBubble = {
...@@ -939,10 +1041,11 @@ function initializeApp(data) { ...@@ -939,10 +1041,11 @@ function initializeApp(data) {
this.translateBubble.result = responseText; this.translateBubble.result = responseText;
} }
// 每次更新后滚动到底部 // 每次更新后滚动到底部并调整位置
this.$nextTick(() => { this.$nextTick(() => {
setTimeout(() => { setTimeout(() => {
this.scrollToBottom(); this.scrollToBottom();
this.adjustBubblePosition(); // 动态调整气泡框位置
}, 10); }, 10);
}); });
} }
...@@ -962,6 +1065,7 @@ function initializeApp(data) { ...@@ -962,6 +1065,7 @@ function initializeApp(data) {
} }
this.scrollToBottom(); this.scrollToBottom();
this.adjustBubblePosition(); // 最终位置调整
console.log('翻译完成:', responseText); console.log('翻译完成:', responseText);
resolve(); resolve();
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment