@@ -114,18 +114,20 @@ function main() {
114
114
const mouseX = event . pageX || event . clientX ;
115
115
const mouseY = event . pageY || event . clientY ;
116
116
117
- // Calculate tooltip width (default to 320px if not rendered yet)
117
+ // Calculate tooltip dimensions (default to 320px width if not rendered yet)
118
118
let tooltipWidth = 320 ;
119
+ let tooltipHeight = 200 ;
119
120
if ( this . _tooltip && this . _tooltip . node ( ) ) {
120
121
const node = this . _tooltip
121
122
. style ( "opacity" , 0 )
122
123
. style ( "display" , "block" )
123
124
. node ( ) ;
124
125
tooltipWidth = node . offsetWidth || 320 ;
126
+ tooltipHeight = node . offsetHeight || 200 ;
125
127
this . _tooltip . style ( "display" , null ) ;
126
128
}
127
129
128
- // Calculate position: if overflow, show to the left of cursor
130
+ // Calculate horizontal position: if overflow, show to the left of cursor
129
131
const padding = 10 ;
130
132
const rightEdge = mouseX + padding + tooltipWidth ;
131
133
const viewportWidth = window . innerWidth ;
@@ -137,10 +139,21 @@ function main() {
137
139
left = mouseX + padding ;
138
140
}
139
141
142
+ // Calculate vertical position: if overflow, show above cursor
143
+ const bottomEdge = mouseY + padding + tooltipHeight ;
144
+ const viewportHeight = window . innerHeight ;
145
+ let top ;
146
+ if ( bottomEdge > viewportHeight ) {
147
+ top = mouseY - tooltipHeight - padding ;
148
+ if ( top < 0 ) top = padding ; // prevent off top edge
149
+ } else {
150
+ top = mouseY + padding ;
151
+ }
152
+
140
153
this . _tooltip
141
154
. html ( tooltipHTML )
142
155
. style ( "left" , left + "px" )
143
- . style ( "top" , mouseY - 10 + "px" )
156
+ . style ( "top" , top + "px" )
144
157
. transition ( )
145
158
. duration ( 200 )
146
159
. style ( "opacity" , 1 ) ;
0 commit comments