简体   繁体   English

顶部父元素的底部元素位置

[英]Position bottom element of top parent element

I am making this simple tooltip where I want to position the tooltip bottom at the top of the parent element. 我正在制作此简单的工具提示,我希望将工具提示的底部放在父元素的顶部。 I want to do this by getting the height of the tooltip element and set this number negative to the top positioning. 我想通过获取工具提示元素的高度并将此数字设置为负数来定位顶部。 The problem is that at the time that I hover the element, the tooltip height is 0, according to console.log(); 问题是,根据console.log();当我将元素悬停时,工具提示高度为0。

 $('.tooltip').hover(function() { var content = $(this).data('tip-content'); var element = $(this).find('.tip-content'); if(element.length == 0 ) { var html = $('<p class="tip-content">' + content + '</p>'); var height = html.height(); console.log(height); html.css('top', - height); $(this).prepend(html); } else { element.remove(); } }); 
 .element { height: 50px; width: 50px; margin: 50px auto; background: #000; } .tooltip { position: relative; } .tooltip .tip-content { width: 180px; margin-left: -98px; padding: 10px 5px; position: absolute; left: 50%; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; background: #294a72; font-size: 0.75em; color: #fff; text-align: center; } .tooltip .tip-content:after { top: 100%; left: 50%; border: solid transparent; content: " "; height: 0; width: 0; position: absolute; pointer-events: none; border-top-color: #294a72; border-width: 5px; margin-left: -5px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="element tooltip" data-tip-content="This is a test content"> </div> 

At the time you're checking the height, the element has not yet been added to the DOM, and therefore can have no height. 在检查高度时,该元素尚未添加到DOM中,因此可以没有高度。 You simply need to switch the order of your statements. 您只需要切换语句的顺序即可。 jQuery can and will change the CSS of the element even after it has been added. 即使添加了jQuery,jQuery也会并且会更改元素的CSS。

var html = $('<p class="tip-content">' + content + '</p>');
$(this).prepend(html);  //This line must go before the next
var height  = html.height();
console.log(height);

However, you're still missing some pieces. 但是,您仍然缺少一些片段。 height() does not include either margin or padding. height() 包括任何保证金或填充。 To get padding, you can use outerHeight() , but margin you'll have to either read from the CSS or use a hard-coded value. 要填充,您可以使用outerHeight() ,但是必须从CSS读取或使用硬编码值来保证页边空白。 Even worse, your arrow is using a pseudo-element, which *cannot* be read by DOM traversal, so your best bet there is to just hardcode it, sadly. 更糟的是,你的箭头使用伪元素,*不能*被DOM遍历读,所以最好的办法是有,只是硬编码,可悲。

A better height calculation might look like: 更好的高度计算可能类似于:

var ARROW_HEIGHT = 5;
html.outerHeight() + parseInt(html.css('marginBottom'), 10) + ARROW_HEIGHT;

I think you have to prepend the HTML, and then get the height and reposition the element. 我认为您必须先添加HTML, 然后获取高度并重新定位元素。 Right now, you are getting the height of a variable, not an HTML element. 现在,您正在获取变量的高度,而不是HTML元素的高度。

For html object gets a height automatically, you need first put in DOM. 为使html对象自动获得高度,您需要先放入DOM。 That the reason for you get height = 0. You need first append your object and then get the height. 那就是您获得height = 0的原因。您需要先附加对象,然后获得高度。

See my example: https://jsfiddle.net/bwun82q4/ 参见我的示例: https : //jsfiddle.net/bwun82q4/

$('.tooltip').hover(function() {
var content = $(this).data('tip-content');
var element = $(this).find('.tip-content');

if(element.length == 0 ) {
    var html    = $('<p class="tip-content">' + content + '</p>');
    var height  = html.height();
    console.log(height);
    html.css('top', - height);
    $(this).prepend(html);

    $(this).find("p").css("top",- $(this).find("p").height());
} else {
    element.remove();
}});

You just need to get height of 'tooltip' instead of 'tip-content'. 您只需要获取“工具提示”的高度,而不是“提示内容”的高度即可。

 $('.tooltip').hover(function() { var content = $(this).data('tip-content'); var element = $(this).find('.tip-content'); if(element.length == 0 ) { var html = $('<p class="tip-content">' + content + '</p>'); // Get height of parent element var height = $(this).height(); console.log(height); html.css('top', - height); $(this).prepend(html); } else { element.remove(); } }); 
 .element { height: 50px; width: 50px; margin: 50px auto; background: #000; } .tooltip { position: relative; } .tooltip .tip-content { width: 180px; margin-left: -98px; padding: 10px 5px; position: absolute; left: 50%; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; background: #294a72; font-size: 0.75em; color: #fff; text-align: center; } .tooltip .tip-content:after { top: 100%; left: 50%; border: solid transparent; content: " "; height: 0; width: 0; position: absolute; pointer-events: none; border-top-color: #294a72; border-width: 5px; margin-left: -5px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="element tooltip" data-tip-content="This is a test content"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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