简体   繁体   English

垂直对齐div内容,在mouseover上替换tr元素(使用jQuery)

[英]Vertically align div content, that substitutes tr element on mouseover (using jQuery)

I'm working on a html table where I show more information on a row using jQuery. 我正在开发一个html表,我在其中使用jQuery显示有关行的更多信息。

The following is an example of how I'm doing it: http://jsfiddle.net/rMXAp/7/ 以下是我如何做的一个例子: http//jsfiddle.net/rMXAp/7/

The jQuery essentially fades the row in question, by setting the opacity to 0, and adds an overlay containing the following div: 通过将不透明度设置为0,jQuery基本上淡化了有问题的行,并添加了包含以下div的叠加层:

<div id="divOverlay" style=""><p>This is the overlay div.</p><p id="info"></p></div> 

The jQuery then sets the div text based on the "desc" attribute of the tr/row. 然后jQuery根据tr /行的“desc”属性设置div文本。

The problem I'm having lies in vertical alignment of the text shown (in a div) in place of the table row, when hovering over it. 当我将鼠标悬停在表格行上时,我遇到的问题在于所显示文本的垂直对齐(在div中)代替表格行。

Things I've tried. 我尝试过的事情。

  • "vertical-align:middle;" “垂直对齐:中部;” for the tr element, as well as the div. 对于tr元素,以及div。
  • "min-height: 10em;" “最小高度:10em;” for the div. 对于div。
  • "position: absolute; top: 50%;" “位置:绝对;顶部:50%;” for the div. 对于div。

I cannot set the div's "display" property to anything other than none, or else it would display below the table (see jsfiddle). 我不能将div的“display”属性设置为除none之外的任何其他属性,否则它将显示在表格下方(请参阅jsfiddle)。

I'm aware there are many questions on vertical alignment, but given I'm showing a div using jQuery, and setting the html dynamically, would they be used the same way? 我知道垂直对齐有很多问题,但鉴于我使用jQuery显示div,并动态设置html,它们会以相同的方式使用吗? Or how? 或者怎么样?

I've got the feeling I've completely missed something here... 我有种感觉我在这里完全错过了一些东西......

Ahh.. verical aligning... I think the best method is using the table-cell display. 啊..真正的对齐...我认为最好的方法是使用table-cell显示。

The problem is that for display:table-cell; 问题是display:table-cell; to work properly, the parent must have display:table ; 要正常工作,父母必须有display:table ;

So I'd suggest something like this: 所以我建议这样的事情:

HTML HTML

<div id="divOverlay">
   <div class="content" style="display:table-cell;vertical-align:middle;">
   </div> <!-- the content div will hold the content -->
</div> 

Javascript: 使用Javascript:

// inside onmouseover...
$divOverlay.css({
        position: 'absolute',
        display: 'table', //set parent's display to table
        ....
    }); 
    //set the html to the child content holder
    $divOverlay.find(".content").html($(this).closest('tr').attr('desc')); 

check out this jFiddle: http://jsfiddle.net/SpacePineapple/4T42H/ 看看这个jFiddle: http//jsfiddle.net/SpacePineapple/4T42H/

I guess you don't have any problem by keeping content of "desc" data into a span. 我想通过将“desc”数据的内容保持在一个范围内没有任何问题。

Have took a span having line-height:normal and vertical-align:middle and added the line-height for div. 使用了一个具有line-height:normal and vertical-align:middle的跨度line-height:normal and vertical-align:middleline-height for div.添加了line-height for div.

Edited your fiddle here 在这里编辑你的小提琴

Please let me know if you want something else. 如果你想要别的东西,请告诉我。

EDIT 编辑

If you don't want to change the content of "desc" then you can implement like this too. 如果您不想更改“desc”的内容,那么您也可以这样实现

$divOverlay.html("<span style='vertical-align:middle;display:inline-block;line-height:normal'>"+$(this).closest('tr').attr('desc')+"</span>");

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

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