Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
AI_BOOK_TOOL
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
template admin
AI_BOOK_TOOL
Commits
84d21769
Commit
84d21769
authored
Aug 02, 2025
by
liujiangnan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 调整位置
parent
bf4638d0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
139 additions
and
20 deletions
+139
-20
index.css
play/index.css
+15
-0
index.js
play/index.js
+124
-20
No files found.
play/index.css
View file @
84d21769
...
...
@@ -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
{
...
...
play/index.js
View file @
84d21769
...
...
@@ -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
:
final
Left
+
'
px
'
,
top
:
final
Top
+
'
px
'
,
left
:
bubble
Left
+
'
px
'
,
top
:
bubble
Top
+
'
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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment