【问题标题】:Detecting div height on resize在调整大小时检测 div 高度
【发布时间】:2009-09-16 10:29:24
【问题描述】:

我正在尝试检测我正在加载内容的 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;
                         }
                         });
    });
});

我在加载时在萤火虫中得到这个:

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

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

【问题讨论】:

  • 你在哪些浏览器上测试过这个?

标签: javascript jquery html css resize


【解决方案1】:

除非 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 在访问 height 和 width 属性之前进行排序。你可以选择去掉它,直接将content_loaded作为回调参数传递给load方法。

【讨论】:

  • 与其使用 .css 方法,不如使用 .height 方法改变高度。 JQuery .height 方法接受一个参数,告诉它你想设置什么高度。尝试在 onclick 中使用 .height(0) 。 TBH 我完全不明白你为什么要设置内容类型高度,当然你只需要确保设置间隔高度,因为内容高度会随着每次内容的变化而自然变化。
  • 是的,这就是我想要做的,垫片是一个美学组件,沿着内容绘制右手边框,尽管它在另一个 div 中。我不太确定该怎么做。可以在这里看到:samuelfarfsing.com/test.php
  • 感谢“Fx 不支持在 div 上调整大小” - 但是我正在查看的代码不能很好地处理点击,因为扩展 div 的动画是在点击之后。我将代码添加到动画的末尾
【解决方案2】:
//Get Content size after load
$("#content").bind("resize", function(){ 
                    $("#content").css({"height": $("#content").height()}); 
                    });

您没有将“px”添加到高度值的末尾

//Get Content size after load
$("#content").bind("resize", function(){ 
                    $("#content").css({"height": $("#content").height() + "px"}); 
                    });

编辑跟随

让它在本地工作,您在点击事件中的高度函数末尾缺少括号:

$("#spacer").css({"height": $("#content").height() + "px"}); //added parenthesis enables the #spacer div to change height accordingly

希望对你有帮助

【讨论】:

  • 如果没有 px,css 通常会采用什么单位?答:像素。
  • 不!无论哪种方式,当您假设您对您和我产生了影响时,根据经验,我发现最好不要让浏览器“假设”任何事情
  • 先生。奥斯汀有一点。现在有多少浏览器渲染引擎? - 我很确定答案很多,他们并不都遵守规则。
  • @AnthonyWJones:我要说的是,实际上,包括 FF3 在内的一些浏览器会忽略最后没有 px 单位的“height:30”样式规则,我有几次忘记将 px 添加到通过 javascript 进行的 css 更改中,令人惊讶的是,在最后添加 +“px”后,它起作用了。见鬼,你自己试试吧!
  • aww,你还是不明白。
【解决方案3】:

我喜欢在未设置时使用 offsetheight 来检测高度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 2014-11-10
    • 2014-01-15
    • 2016-02-01
    相关资源
    最近更新 更多