javascript - CSS 当使用溢出时,差距仍然存在

标签 javascript jquery html css

我创建了一个 HTML 表格,当我使用 overflow 来确保它不超过特定高度时,下面的 div 以文本仍然适合的方式放置。

我需要将 div 直接放在滚动表的下方。

我正在使用 overflow: hidden 但是我仍然可以选择文本,但我看不到它。

$('document').ready(function() {
	var $table = $('#ingsNeededTable'),
    $bodyCells = $table.find('tbody tr:first').children(),
    colWidth;

	$(window).resize(function() {
		colWidth = $bodyCells.map(function() {
			return $(this).width();
		}).get();
		
		$table.find('thead tr').children().each(function(i, v) {
			$(v).width(colWidth[i] + 1);
		});    
	}).resize();
	
	var loop = setInterval(function() {
		$("#ingsNeededTable tbody").animate({scrollTop: $("#ingsNeededTable tbody").prop("scrollHeight")}, 100000, "linear");
		$("#ingsNeededTable tbody").animate({scrollTop: 0}, 100000, "linear");
	}, 1);
});
#ingsNeededTableHolder {
	height: 45%;
}
#ingsNeededTitle, #ingsRecentTitle {
	text-align: center;
}
#ingsNeededTable {
	border-collapse: collapse;
	width: 100%;
}
#ingsNeededTable th, #ingsNeededTable td {
	border-bottom: 1px solid black;
}
#ingsNeededTable thead {
	display: block;
	width: 100%;
}
#ingsNeededTable tbody {
	display: block;
	height: 40%;
	overflow-y: hidden;
	width: 100%;
}
.ingsNeededTableIng, .ingsNeededTableProd {
	width: 40%;
}
.ingsNeededTableNeeded {
	width: 10%
}
#ingsNeededTable tr:nth-child(even) {
	background-color: #eaf2f1
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id='ingsNeededTableHolder'>
  <table id='ingsNeededTable'>
    <thead>
      <tr>
        <th class='ingsNeededTableIng'>Ingredient</th>
        <th class='ingsNeededTableNeeded'>Needed (Kg)</th>
        <th class='ingsNeededTableProd'>Product</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
    </tbody>
  </table>
</div>

<div>
  <h2>This Should Be Below The Scrolling Table</h2>
</div>

表格在我的页面上正确对齐,但由于某种原因它不在代码段上。

最佳答案

将最大高度设置为 #ingsNeededTableHolder

#ingsNeededTableHolder {
    height: 45%;
    max-height: 150px;
    overflow: hidden;
}

$('document').ready(function() {
	var $table = $('#ingsNeededTable'),
    $bodyCells = $table.find('tbody tr:first').children(),
    colWidth;

	$(window).resize(function() {
		colWidth = $bodyCells.map(function() {
			return $(this).width();
		}).get();
		
		$table.find('thead tr').children().each(function(i, v) {
			$(v).width(colWidth[i] + 1);
		});    
	}).resize();
	
	var loop = setInterval(function() {
		$("#ingsNeededTable tbody").animate({scrollTop: $("#ingsNeededTable tbody").prop("scrollHeight")}, 100000, "linear");
		$("#ingsNeededTable tbody").animate({scrollTop: 0}, 100000, "linear");
	}, 1);
});
#ingsNeededTableHolder {
    height: 45%;
    max-height: 150px;
    overflow: hidden;
}
#ingsNeededTitle, #ingsRecentTitle {
	text-align: center;
}
#ingsNeededTable {
	border-collapse: collapse;
	width: 100%;
}
#ingsNeededTable th, #ingsNeededTable td {
	border-bottom: 1px solid black;
}
#ingsNeededTable thead {
	display: block;
	width: 100%;
}
#ingsNeededTable tbody {
	display: block;
	height: 40%;
	overflow-y: hidden;
	width: 100%;
}
.ingsNeededTableIng, .ingsNeededTableProd {
	width: 40%;
}
.ingsNeededTableNeeded {
	width: 10%
}
#ingsNeededTable tr:nth-child(even) {
	background-color: #eaf2f1
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id='ingsNeededTableHolder'>
  <table id='ingsNeededTable'>
    <thead>
      <tr>
        <th class='ingsNeededTableIng'>Ingredient</th>
        <th class='ingsNeededTableNeeded'>Needed (Kg)</th>
        <th class='ingsNeededTableProd'>Product</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
      <tr>
        <td class='ingsNeededTableIng'>foo</td>
        <td class='ingsNeededTableNeeded'>foo</td>
        <td class='ingsNeededTableProd'>foo</td>
      </tr>
    </tbody>
  </table>
</div>

<div>
  <h2>This Should Be Below The Scrolling Table</h2>
</div>

关于javascript - CSS 当使用溢出时,差距仍然存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40103642/

相关文章:

javascript - "Point added"事件,同时使用谷歌地图 API 绘制多边形

javascript - Discord Bot - 无法从意图访问标志

jquery - 如果用户在提交时未更改<select> <option>,如何添加消息

html - margin-top in parent, negative top in child, 如何摆脱下面的剩余高度

html - Bootstrap 下拉列表 :hover

javascript - 如何为 2 个子 div 添加包装器 div(所有子 div 具有相同的类)

javascript - 主干路由器 + RewriteRule 不触发带有 "/"的路由

php - jquery 链式自动完成

javascript - 为什么我不能删除段落?

javascript - jquery get 语法与 php 和 mysql 进行输入验证