gpt4 book ai didi

javascript - 在调整大小时检测 div 高度

转载 作者:可可西里 更新时间:2023-11-01 13:18:18 26 4
gpt4 key购买 nike

我正在尝试检测正在加载内容的 div 的高度。这个 div 没有指定的高度,因为我正在将页面加载到其中并且它们自己以不同的数量填充 div。我认为我使用的代码不起作用。

#content divdocument load 上获得了正确的高度,但是在单击 load 事件时我无法获得高度。

html:

<div id="select">
<div id="menu">
<ul>
<li><a class="load" href="javascript:void(0)" id="p1">P1</a></li>
<li><a class="load" href="javascript:void(0)" id="p2">P2</a></li>
</ul>
</div>
<div id="spacer"></div>
<div id="content"></div>

CSS:

#content {
left: 227px;
top: 20px;
width: 703px;
padding: 0 0 100px 0;
position: absolute;
}

#spacer {
border-right: 2px solid #000000;
left: 10px;
position: absolute;
top: 710px;
width: 215px;
}

我的 jquery:

$(document).ready(function() {
//Load Initial Content
$("#content").html('<ul><li>Loading Content...</li></ul>')
.load("/content/" + "projects.php", null, function() {
contentHeight = $("#content").height();
selectHeight = $("#select").height();

$("#content").height(contentHeight);
$("#select").height(selectHeight);
$("#spacer").height((contentHeight - selectHeight - 40) + "px")
.css({"top": selectHeight + 40 + "px" });
});

//Load content on click function
$(".load").click(function(){
loadName = $(this).attr("id");
$("#content").html('<ul><li>Loading Content...</li></ul>')
.load("/content/" + loadName + ".php", null, function(){
contentHeight = $("#content").height();
selectHeight = $("#select").height();

$("#spacer").height(0);

if(selectHeight > contentHeight) {
$("#spacer").css({"display": "none"});
}else{

$("#spacer").css({"top": selectHeight + 40 + "px", "display": "block" })
.height((contentHeight - selectHeight - 40) + "px");
return false;
}
});
});
});

我在 firebug 中加载这个:

<div id="select" style="height: 689px;"/>
<div id="spacer" style="height: 5461px; top: 729px;"/>
<div id="content" style="height: 6190px;"/>

现在如果我点击 P2,内容高度的 div 保持不变,即使 div 中的实际内容只有 625px 高;所以它不会被切换。

最佳答案

除非 JQuery 非常聪明(它很聪明,但我认为它没有这么聪明),否则您不会获得调整大小事件,因为 firefox(可能还有 Safari)仅支持窗口对象上的调整大小事件。 IE 确实支持 DIV 等元素的调整大小事件。

因此您需要一种不同的方法:-

$(document).ready(function()
{
$("#content").html('<ul><li>Loading Content...</li></ul>')
.load("/content/" + "projects.php", null, function()
{
window.setTimeout(content_loaded, 0);
});

function content_loaded()
{
$("#content").css({"height": $("#content").height() + "px"});
}

$("#spacer").css({"height": $("#content").height() + "px"});

$(".load").click(function()
{
loadName = $(this).attr("id");

$("#content").html('<ul><li>Loading Content...</li></ul>')
.load("/content/" + loadName + ".php", null, function()
{
window.setTimeout(content_loaded, 0); });
});

$("#spacer").css({"height": $("#content").height + "px"});
});
});

注意我这里使用setTimeout方法是因为当内容发生变化时IE在整理宽高参数时经常会卡顿。通过使用 setTimeout,它可以让 IE 在访问高度和宽度属性之前解决问题。您可以选择将其删除并将 content_loaded 作为回调参数直接传递给加载方法。

关于javascript - 在调整大小时检测 div 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1432171/

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