简体   繁体   English

如何基于目标元素动态更改绝对工具提示的位置

[英]How to dynamically change the position of an absolute tooltip base on the target element

I am trying to make a tooltip which basically shows a table (in my example below I used a large amount of text). 我试图制作一个基本上显示表格的工具提示(在下面的示例中,我使用了大量文本)。 However I've wanted to change the position of the tooltip when you hover on the target element that is nearly at the corner of the screen. 但是,当您将鼠标悬停在屏幕拐角处的目标元素上时,我想更改工具提示的位置。

Here is the Fiddle 这是小提琴

$("strong").on("mouseover", function(){
var $this = $(this),
strongText = $this.text();
$tooltipContainer.show();
$tooltipContainer.append('<span>'+ strongText + '</span>');
}).on("mousemove", function(mousePos){...

You can change your mousemove code a little to update top position if overlap is there like below. 如果出现重叠,您可以稍微更改一下mousemove代码以更新top位置。 Check demo - Fiddle 查看演示- 小提琴

on("mousemove", function(mousePos){
    var overlap = mousePos.pageY + posSrollY + $tooltipContainer.height() - $(window).height() - $(window).scrollTop();
    $tooltipContainer.css({
        left: mousePos.pageX,
        top: mousePos.pageY + posSrollY - ( overlap > 0 && overlap )
    });
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM