我想叠加一个div(或者任何可以工作的元素)在一个只有一列的表格行(tr标签)上。如何在表格行(tr)上覆盖div(或任何元素)?
我尝试了几种方法,这似乎并不奏效。我已经在下面发布了我的当前代码。
我得到一个覆盖,但不是直接在行上。我尝试将覆盖顶部设置为$ divBottom.css('top'),但这总是'auto'。
那么,我在正确的轨道上,还是有更好的方式呢?你可以看到,使用jQuery是很好的。
如果我在正确的轨道上,如何正确放置div? offsetTop是包含元素,表中的偏移量,我需要做一些数学计算?我会遇到任何其他陷阱?
$(document).ready(function() {
$('#lnkDoIt').click(function() {
var $divBottom = $('#rowBottom');
var $divOverlay = $('#divOverlay');
var bottomTop = $divBottom.attr('offsetTop');
var bottomLeft = $divBottom.attr('offsetLeft');
var bottomWidth = $divBottom.css('width');
var bottomHeight = $divBottom.css('height');
$divOverlay.css('top', bottomTop);
$divOverlay.css('left', bottomLeft);
$divOverlay.css('width', bottomWidth);
$divOverlay.css('height', bottomHeight);
$('#info').text('Top: ' + bottomTop + ' Left: ' + bottomLeft);
});
});
#rowBottom { outline:red solid 2px }
#divBottom { margin:1em; font-size:xx-large; position:relative; }
#divOverlay { background-color:Silver; text-align:center; position:absolute; z-index:10000; opacity:0.5; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
<title>Overlay Tests</title>
</head>
<body>
<p align="center"><a id="lnkDoIt" href="#">Do it!</a></p>
<table width="100%" border="0" cellpadding="10" cellspacing="3" style="position:relative">
<tr>
<td><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p></td>
</tr>
<tr id="rowBottom">
<td><div id="divBottom"><p align="center">This is the bottom text</p></div></td>
</tr>
<tr>
<td><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p></td>
</tr>
</table>
<div id="divOverlay" style=""><p>This is the overlay div.</p><p id="info"></p></div>
</body>
</html>
他过于已拿起“绝对位置”。然而,你正确地使用“pos()”。 – Pointy 2010-06-17 20:10:16
这似乎是做伎俩......有一个变化: var rowPos = $ divBottom.pos(); 应该是 var rowPos = $ divBottom.offset(); .offset()是文档中的顶部/左侧。 – slolife 2010-06-17 20:40:26
jquery没有名为'pos'的函数。正确的语法是$ divBottom.position(); – 2011-07-27 09:48:03