javascript - jquery 工具提示应该在屏幕外时改变位置

标签 javascript jquery mouseover offset off-screen

在窗口滚动时,工具提示应正确显示在其父级的上方/下方/左侧/右侧。 当我向下滚动演示时,工具提示的位置就会停止。

如何计算父级的 y 偏移量并在正确的位置显示工具提示? 我快到了,不知道还缺少什么。 http://fiddle.jshell.net/j7MWE/

 $.fn.tooltip = function () {
     var $el = $(this);
     var $w = $(window);
     var timer;
     var delay = 500;

     $el.mouseenter(function (e) {
         timer = setTimeout(function () {
             var $c = $(e.currentTarget);
             var $tt = $('<div class="tooltip fade right"><div class="arrow"></div><h3 class="popover-title" style="display: none;"></h3><div class="popover-content"><article class="default"><h1>Anchorman 2: The Legend Continues</h1><ul><button>£10.99 Buy</button><button>£3.49 Rent</button><p>Hilarious comedy sequel starring Will Ferrell and Steve Carell.</p></article></div></div>').appendTo($(e.currentTarget).closest('.item')).fadeIn(300);

             $tt.toggleClass('horiz-offscreen', $w.width() < $tt.outerWidth() + $tt.offset().left);
             if ($w.height() < $tt.outerHeight() + $tt.offset().top) {
                 $tt.css('top', $w.scrollTop() + $w.height() - $c.position().top - $tt.outerHeight());
             }
         }, delay);
     });

     $el.mouseleave(function (e) {
         $('.tooltip', e.currentTarget).fadeOut(500, function () {
             $(this).remove();
         });
         clearTimeout(timer);
     });

 };

 $('.item').tooltip();

最佳答案

它很旧,但很有帮助,

计算你可以使用这个:

$tt.css('top', abs($c.position().top - $w.scrollTop() - $tt.outerHeight()) );

$c.position().top 元素距顶部的距离

$w.scrollTop() 滚动值

$tt.outerHeight() 你的工具提示高度

然后我们做一些数学运算并得到绝对值

关于javascript - jquery 工具提示应该在屏幕外时改变位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23998522/

相关文章:

javascript - leaflet.js 1.0-rc 网格层。缩放时不重画 Canvas 圆

jquery - 在可编辑的 div 中“选择”图像

javascript - Html5 和 Css 如何创建单独的 ul li 并将 <aside> 移到左侧

javascript - 在 OpenLayers 中为鼠标悬停和选择设置不同的格式?

javascript - 如何清除内容 div 并使用 javascript 重新出现

javascript - 如何在 JavaScript 中迭代(键、值)?

javascript - 动态创建表单栏杆

javascript - jQuery 不选择 div 类

javascript - 将按钮 onmouseover 更改为 onclick

css - 将图片悬停位置与文本一起更改