I'm encountering an issue with positioning a tooltip that I'm creating.
The tooltip's CSS is set to position: absolute
, and I've implemented a mouse event handler that adjusts its top
and left
based on the pageX
and pageY
.
While I could simply set the top
as pageY
and left
as pageX
, this would cause the tooltip to always appear at the bottom-right. My goal is to have it show up at the top-right if space allows, or fallback to the bottom-right position if it goes beyond the viewport on the Y-axis.
Currently, I'm struggling to figure out how to display the tooltip at the top-right of the mouse cursor. I'm unsure where to start in terms of detecting if it would be within the viewport. Can anyone provide guidance?
$('p').on('mouseenter', function(e) {
$(tt).css('top', e.pageY - $(tt).css('height'));
$(tt).css('left', e.pageX);
$(tt).appendTo('body');
}).on('mousemove', function(e) {
$(tt).css('top', e.pageY - $(tt).css('height'));
$(tt).css('left', e.pageX);
}).on('mouseleave', function(e) {
$(tt).detach();
});