javascript - 使用 jQuery 使绝对 div 与另一个 div 的顶部位置相同

标签 javascript jquery html css

我需要一些 CSS/jQuery 专家。

基本上,有 2 个父 div 并排放置。第一个父 div 包含一张图片,下面包含一个表格。 第二个父 div 仅包含一个样式为 position: absolute; 的表格。

现在的问题是,图片的高度不同,这意味着它下面的 table 的位置会发生变化。

因此,如何在不更改 HTML 结构的情况下使第二个 div 上的表格与第一个表格对齐。是否可以使用 jQuery 来实现,例如获取第一个表格位置并将其应用于第二个表格?

基本上这里的结构是:

.picture {
  background-color: #000;
  width: 550px;
}

.second-table {
  position: absolute;
  top: 385px;
}
<div class="div1" style = "width: 30%; display: inline-block;">
  <div class="picture" style="height: 378px;">
  </div>
  <table class="shop_attributes1" style="background-color: #686868; margin-top: 30px;">
			<tbody><tr class="">
			<th>Model</th>
			<td><p>Legend Diver</p>
</td>
		</tr>
			<tr class="alt">
			<th>Reference</th>
			<td><p>L3.674.4.50.0</p>
</td>
		</tr>
			<tr class="">
			<th>Gender</th>
			<td><p>Mens</p>
</td>
		</tr>
			<tr class="alt">
			<th>Case Size</th>
			<td><p>42.0 mm</p>
</td>
		</tr>
			<tr class="">
			<th>Case Material</th>
			<td><p>Stainless steel</p>
		</tr>
	    </tbody></table>
</div>

<div class="div2" style = "width: 50%; display: inline-block;">
  <table class="shop_attributes2 second-table" style="background-color: #686868; margin-top: 30px;">
			<tbody><tr class="">
			<th>Model</th>
			<td><p>Legend Diver</p>
</td>
		</tr>
			<tr class="alt">
			<th>Reference</th>
			<td><p>L3.674.4.50.0</p>
</td>
		</tr>
			<tr class="">
			<th>Gender</th>
			<td><p>Mens</p>
</td>
		</tr>
			<tr class="alt">
			<th>Case Size</th>
			<td><p>42.0 mm</p>
</td>
		</tr>
			<tr class="">
			<th>Case Material</th>
			<td><p>Stainless steel</p>
		</tr>
	    </tbody></table>
</div>

最佳答案

您可以为此使用 jquery,是的。像这样的东西:

var test = $( ".shop_attributes1" );
var position = test.position();
$('.second-table').css('top', position.top);

这是一个 jsfiddle:https://jsfiddle.net/7wvjra83/1/

基本上,您占据第一个表格的位置,并为第二个表格设置要对齐的 css。希望这对你有用

关于javascript - 使用 jQuery 使绝对 div 与另一个 div 的顶部位置相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52968693/

相关文章:

javascript - 停止代码直到 AJAX 调用在 $.each 循环内完成

javascript - 如何在 Firefox 中将 Canvas 扩展到整个视口(viewport)?

javascript - addEventListener 在 Javascript 中不起作用,但 jQuery Click 正在工作

html - 呈现 HTML(转换为位图)

html - 如何使用 margin 属性使 <ul> 居中

javascript - 通过循环访问数组中对象的属性时,仅获取第一个元素

javascript - 检查一个CSS动画是用JQuery还是JS完成的?

jquery - 使用 jQuery 更改背景颜色。重置为默认

javascript - 单击动态链接时填充 jQuery Mobile 页面内容

javascript - 甜蜜警报 2 和 javascript :How do i make a Sweet alert button that sends me to a certain webpage when clicked