繁体   English   中英

jQuery工具提示应在屏幕外时更改位置

[英]jquery tooltip should change position when offscreen

在窗口上滚动时,工具提示应正确显示在其父对象的上方/下方/左侧/右侧。 在我的演示上向下滚动后,Tooltip的位置就会刹车。

如何计算父项的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()您的工具提示高度

然后我们进行一些数学运算并获得绝对值

暂无
暂无

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

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