Commit 84d21769 authored by liujiangnan's avatar liujiangnan

feat: 调整位置

parent bf4638d0
......@@ -277,12 +277,27 @@ body {
position: absolute;
width: 600px;
max-height: 400px;
min-height: 200px;
background-color: white;
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
border: 1px solid #e0e0e0;
z-index: 1000;
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 {
......
......@@ -86,7 +86,7 @@ function initializeApp(data) {
}
},
mounted() {
mounted() {
// 根据 staging 字符串,判断是否为 staging 环境
const isStaging = window.location.href.includes('staging');
const isLocalhost = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1';
......@@ -101,14 +101,18 @@ function initializeApp(data) {
console.log('Vue组件已挂载,数据:', this.bookData);
this.initAudio();
this.loadCurrentPage();
// 监听窗口大小变化,重新计算高亮位置
window.addEventListener('resize', this.onWindowResize);
// 监听窗口大小变化,调整气泡框位置
window.addEventListener('resize', this.adjustBubblePosition);
},
beforeDestroy() {
this.cleanup();
window.removeEventListener('resize', this.onWindowResize);
window.removeEventListener('resize', this.adjustBubblePosition);
},
methods: {
......@@ -841,38 +845,136 @@ function initializeApp(data) {
this.requestTranslation(word);
},
// 计算气泡框位置
// 计算气泡框位置
calculateBubblePosition(wordStyle) {
const imgElement = document.querySelector('.page-image');
if (!imgElement) return {};
const imgRect = imgElement.getBoundingClientRect();
const containerRect = imgElement.parentElement.getBoundingClientRect();
// 计算单词在容器中的位置
const wordLeft = parseFloat(wordStyle.left);
const wordTop = parseFloat(wordStyle.top);
const wordWidth = parseFloat(wordStyle.width);
const wordHeight = parseFloat(wordStyle.height);
// 气泡框位置(在单词右侧)
const bubbleLeft = wordLeft + wordWidth + 10;
const bubbleTop = wordTop - 20;
// 确保气泡框不超出容器边界
const maxLeft = containerRect.width - 600; // 气泡框宽度约600px
const maxTop = containerRect.height - 400; // 气泡框高度约400px
const finalLeft = Math.min(bubbleLeft, maxLeft);
const finalTop = Math.max(bubbleTop, 10);
// 获取屏幕尺寸
const screenWidth = window.innerWidth;
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:优先在单词右侧
bubbleLeft = wordLeft + wordWidth + 10;
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 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 {
position: 'absolute',
left: finalLeft + 'px',
top: finalTop + 'px',
left: bubbleLeft + 'px',
top: bubbleTop + 'px',
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() {
......@@ -939,10 +1041,11 @@ function initializeApp(data) {
this.translateBubble.result = responseText;
}
// 每次更新后滚动到底部
// 每次更新后滚动到底部并调整位置
this.$nextTick(() => {
setTimeout(() => {
this.scrollToBottom();
this.adjustBubblePosition(); // 动态调整气泡框位置
}, 10);
});
}
......@@ -962,6 +1065,7 @@ function initializeApp(data) {
}
this.scrollToBottom();
this.adjustBubblePosition(); // 最终位置调整
console.log('翻译完成:', responseText);
resolve();
} 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