在窗口滚动时,工具提示应正确显示在其父级的上方/下方/左侧/右侧。 当我向下滚动演示时,工具提示的位置就会停止。
如何计算父级的 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/