【问题标题】:JQuery display element by position - tooltip should change position if parent is offscreenJQuery 按位置显示元素 - 如果父级不在屏幕上,工具提示应更改位置
【发布时间】:2014-06-03 09:20:41
【问题描述】:

我的工具提示显示正确,但如果我向下滚动工具提示会偏移刹车。

如果这是在屏幕外,我如何计算父级的偏移位置?

工具提示应正确显示在屏幕上! DEMO

jquery:

$.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 tooltip mouseover offset


    【解决方案1】:

    试试这个!你需要用 css 来做一些事情。 http://fiddle.jshell.net/j7MWE/3/

    function isScrolledIntoView(elem) {
              var docViewTop = $(window).scrollTop(),
                  docViewBottom = docViewTop + $(window).height(),
    
                  elemTop = $(elem).offset().top,
                  elemBottom = elemTop + $(elem).height(),
    
                  result = 0;
    
              if (elemBottom > docViewBottom) {
                  result = 1;
              } else if (elemTop < docViewTop) {
                  result = -1;
              } 
    
              return result;
          };
         $el.mouseenter(function (e) {
             timer = setTimeout(function () {
                 var $c = $(e.currentTarget);
                 var content = $c.data('content');
    
                 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).hide().fadeIn(500);
    
                 $tt.toggleClass('horiz-offscreen', $w.width() < $tt.outerWidth() + $tt.offset().left);
    
    
                 if (isScrolledIntoView($c) < 0) {
                     $tt.css('top', $w.scrollTop() + 120 - $c.offset().top);
                 } else if (isScrolledIntoView($c) > 0) {
                     $tt.css('top', $w.scrollTop() + $w.height() - $c.offset().top - $tt.outerHeight());
                 }
             }, delay);
         });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 2015-01-03
      • 2014-03-10
      • 1970-01-01
      • 2012-11-12
      相关资源
      最近更新 更多