简体   繁体   中英

jquery tooltip should change position when offscreen

on window scrolling the Tooltip should appear correctly above/below/left/right of its parent. Once I scrolled down on my Demo the position of the Tooltip brakes.

How can I calculate the y-offset of the parent and display the tooltip in the right position? I'm nearly there, not sure what is missing. 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();

its old but it can helps,

to calculate that you can use this:

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

$c.position().top the element distance from top

$w.scrollTop() scrolled value

$tt.outerHeight() your tooltipheight

and than we do some Maths and get the absolute value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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