【问题标题】:jquery tooltip should change position when offscreen离屏时jquery工具提示应该改变位置
【发布时间】:2014-06-02 15:51:43
【问题描述】:

在窗口滚动时,工具提示应正确显示在其父级的上方/下方/左侧/右侧。 一旦我在我的演示上向下滚动,工具提示刹车的位置。

如何计算父级的 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();

【问题讨论】:

    标签: javascript jquery mouseover offset off-screen


    【解决方案1】:

    它很旧,但它可以提供帮助,

    计算你可以使用这个:

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

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

    $w.scrollTop()滚动值

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 1970-01-01
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      相关资源
      最近更新 更多