gpt4 book ai didi

javascript - 如何找到未显示的元素的高度

转载 作者:太空狗 更新时间:2023-10-29 13:10:46 25 4
gpt4 key购买 nike

我正在编写一个网页,其中有一个表格,其中的行可以滑动打开和关闭。最初,一些行是关闭的 (display: none),我希望它们滑动打开。设置高度和使用 overflow: hidden 对表格行不起作用,所以我要更改表格内 div 的高度。

这行得通。唯一的问题是在滑动打开之前我需要知道 div 的高度,这似乎是不可能的。我能想到的一种解决方案是加载显示行的页面,然后遍历它们,存储它们的高度并隐藏它们。我不喜欢这个解决方案,因为页面在加载时会跳来跳去。

这是我的问题的一个简单、可运行的示例。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
table, td {border: 1px solid black;}
#lower_row {display: none;}
#lower_div {overflow: hidden;}
</style>
<script type="text/javascript">
function toggleLower() {
lowerRow = document.getElementById("lower_row");
lowerDiv = document.getElementById("lower_div");
if (getStyle(lowerRow, "display") == "none") {
lowerRow.style.display = "table-row";
}
else {
lowerRow.style.display = "none";
}
showHeight();
}
function showHeight() {
lowerDiv = document.getElementById("lower_div");
document.getElementById("info").innerHTML = getStyle(lowerDiv, "height");
}
// Return a style atribute of an element.
// J/S Pro Techniques p136
function getStyle(elem, name) {
if (elem.style[name]) {
return elem.style[name];
}
else if (elem.currentStyle) {
return elem.currentStyle[name];
}
else if (document.defaultView && document.defaultView.getComputedStyle) {
name = name.replace(/([A-Z])/g, "-$1");
name = name.toLowerCase();
s = document.defaultView.getComputedStyle(elem, "");
return s && s.getPropertyValue(name);
}
else {
return null;
}
}
</script>
</head>

<body onload="showHeight()">
<p>The height the lower row is currently <span id="info"></span></p>
<table>
<tr id="upper_row" onclick="toggleLower()"><td><p>Click me to toggle the next row.</p></td></tr>
<tr id="lower_row"><td><div id="lower_div"><p>Peekaboo!</p></div></td></tr>
</table>

</body>
</html>

编辑 1:

一个建议的解决方案是将 div 移出页面。我无法让它工作,而且我认为它的高度不正确,因为它的高度取决于表格的宽度。

我正在研究使用visibility:hidden 的解决方案,但它有问题。它仍然占用少量空间,并且报告的高度是错误的。这是该解决方案的示例:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
table {width: 250px;}
table, td {border: 1px solid black;}
#lower_row {position: absolute; visibility: hidden}
#lower_div {overflow: hidden;}
</style>
<script type="text/javascript">
function toggleLower() {
lowerRow = document.getElementById("lower_row");
lowerDiv = document.getElementById("lower_div");
if (getStyle(lowerRow, "visibility") == "hidden") {
lowerRow.style.visibility = "visible";
lowerRow.style.position = "static";
}
else {
lowerRow.style.visibility = "hidden";
lowerRow.style.position = "absolute";
}
showHeight();
}
function showHeight() {
lowerDiv = document.getElementById("lower_div");
document.getElementById("info").innerHTML = getStyle(lowerDiv, "height");
}
// Return a style atribute of an element.
// J/S Pro Techniques p136
function getStyle(elem, name) {
if (elem.style[name]) {
return elem.style[name];
}
else if (elem.currentStyle) {
return elem.currentStyle[name];
}
else if (document.defaultView && document.defaultView.getComputedStyle) {
name = name.replace(/([A-Z])/g, "-$1");
name = name.toLowerCase();
s = document.defaultView.getComputedStyle(elem, "");
return s && s.getPropertyValue(name);
}
else {
return null;
}
}
</script>
</head>

<body onload="showHeight()">
<p>The height the lower row is currently <span id="info"></span></p>
<table>
<tr id="upper_row" onclick="toggleLower()"><td><p>Click me to toggle the next row.</p></td></tr>
<tr id="lower_row"><td><div id="lower_div"><p>This is some long text. This is some long text. This is some long text. This is some long text.</p></div></td></tr>
</table>

</body>
</html>

编辑 2:解决方案

Paul 的回答解决了我的问题:如何找到未显示的元素的高度。但是,它不适用于我的问题。在我的网站上,div 的高度取决于它的宽度,这取决于 td 的宽度,这取决于其他行的状态和表格的宽度,这取决于页面的宽度。这意味着,即使我预先计算了高度,一旦有人展开另一行或更改窗口大小,该值就会出错。此外,复制表格并保留所有这些约束几乎是不可能的。

但是,我找到了解决办法。当用户点击展开一行时,我的网站会按顺序执行以下步骤:

  1. 将 div.style.height 设置为 1px。
  2. 将 row.style.display 设置为 table-row。
  3. 存储 div.scrollHeight 的值。
  4. 运行滚动动画,停在 div.scrollHeight。
  5. 动画结束后,设置div.style.height为auto。

div.scrollHeight 给出了 div 内容的高度,包括它的溢出。当不显示 div 时它不起作用,但这对我的应用程序来说不是问题。这是实际代码示例。 (同样,我没有包含滚动动画的代码,因为它太长了。)

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
table, td {border: 1px solid black;}
#lower_row {display: none;}
#lower_div {overflow: hidden;}
</style>
<script type="text/javascript">
function toggleLower() {
var lowerRow = document.getElementById("lower_row");
var lowerDiv = document.getElementById("lower_div");
if (getStyle(lowerRow, "display") == "none") {
lowerDiv.style.height = "0px";
lowerRow.style.display = "table-row";
showHeight();
lowerDiv.style.height = "auto";
}
else {
lowerDiv.style.height = "0px";
showHeight();
lowerRow.style.display = "none";
}
}
function showHeight() {
var lowerDiv = document.getElementById("lower_div");
document.getElementById("info").innerHTML = lowerDiv.scrollHeight;
}
// Return a style atribute of an element.
// J/S Pro Techniques p136
function getStyle(elem, name) {
if (elem.style[name]) {
return elem.style[name];
}
else if (elem.currentStyle) {
return elem.currentStyle[name];
}
else if (document.defaultView && document.defaultView.getComputedStyle) {
name = name.replace(/([A-Z])/g, "-$1");
name = name.toLowerCase();
s = document.defaultView.getComputedStyle(elem, "");
return s && s.getPropertyValue(name);
}
else {
return null;
}
}
</script>
</head>

<body>
<p>The height the lower row is currently <span id="info">...</span></p>
<table>
<tr id="upper_row" onclick="toggleLower()"><td><p>Click me to toggle the next row.</p></td></tr>
<tr id="lower_row"><td><div id="lower_div"><p>
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
This is some long text. This is some long text. This is some long text. This is some long text.
</p></div></td></tr>
</table>

</body>
</html>

最佳答案

你可以复制带有内容的div,放到绝对定位的body中 top:-10000; left:-10000; 所以它将在可见区域之外,然后你可以计算高度并从 DOM 中删除克隆。

更新

或者,如果您以动态方式添加元素,您可以将其设置为display:blockposition:absolutevisibility:hidden - 但你必须确保它不会改变页面上任何元素的位置。 visibility:hidden - 不会显示元素,但会计算它的尺寸(与 display: none 对比)

更新

在您的特定情况下,您的 parent 会对 child 的尺寸产生影响,因此您需要将您的元素克隆到可见区域之外的“相似”父元素中。说“相似”是指它应该具有相同的尺寸,但一般来说 - 样式以及与其尺寸相关的所有内容:

var wrapper = $('<div></div>').appendTo('body').css('display', 'block').css('position', 'absolute').css('top', -10000).css('left', -10000).css('width', $('table').css('width'));
var clone = $('#lower_div').clone().appendTo(wrapper);
document.getElementById("info").innerHTML = clone.height();

这里正在工作jsfiddle给你。

关于javascript - 如何找到未显示的元素的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8186889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com