我一直在研究一个简单的工具提示(请参阅下面的 fiddle ),但遇到了一些定位问题。我希望提示显示在点击链接的中央和上方。此时左上角位于鼠标点击处。我试图将这个位置抵消一半的工具提示,但没有成功。
最佳答案
在 http://jsfiddle.net/CBf4C/3/ 查看更改
您需要获取点击元素的位置(使用.position()
),使用.outerWidth()
获取工具提示的尺寸|和 .outerHeight()
然后根据这些计算..
实际代码是
$('a[title]').click(function(e) {
//fadetooltip and display information
$('body').append('<div class="tooltip"><div class="tipBody"></div></div>');
tip = $(this).attr('title');
var tooltip = $('.tooltip'); // store a reference to the tooltip to use it later
tooltip.fadeTo(300, 0.9).children('.tipBody').html( tip );
// calculate position of tooltip
var el = $(this),
pos = el.position(), // get position of clicked element
w = el.outerWidth(), // get width of clicked element, to find its center
newtop = pos.top - tooltip.outerHeight() , // calculate top position of tooltip
newleft = pos.left + (w/2) - (tooltip.outerWidth()/2); // calculate left position of tooltip
//set position
$('.tooltip').css('left', newleft ) ;
$('.tooltip').css('top', newtop );
hideTip = false;
});
关于javascript - JQuery 工具提示位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12491197/