【问题标题】:Dynamically change div's height to parent将div的高度动态更改为父级
【发布时间】:2013-08-18 19:44:56
【问题描述】:

我怎样才能实现,蓝色框填充 div 的剩余位置并在绿色框变大时动态降低他的高度?

http://jsfiddle.net/trek711/ncrqb/4/

HTML

<div id="wrap">
    <div id="left"></div>
    <div id="righttop">
        <div id="click">Click!</div>
        <div id="box"></div>
        <div id="place"></div>
    </div>
    <div id="rightbot"></div>
</div>

CSS

#wrap {
    padding: 5px;
    width: 700px;
    height: 500px;
    background-color: yellow;
    border: 1px solid black;
}
#left {
    float: left;
    margin: 0 5px 0 0;
    width: 395px;
    height: inherit;
    background-color: red;
}
#righttop {
    float: left;
    padding: 5px;
    width: 290px;
    background-color: green;
}
#rightbot {
    float: left;
    margin: 5px 0 0 0;
    width: 300px;
    height: 60%;
    background-color: blue;
}
#click {
    width: 50px;
    height: 50px;
    background-color: red;
}
#box {
    display: none;
    width: 200px;
    height: 100px;
    background-color: white;
}
#place {
    width: 100px;
    height: 120px;
    background-color: lightgrey;
}

JS

$("#click").on("click", function (e) {
    $("#box").slideToggle("fast");
});

非常感谢!

【问题讨论】:

标签: jquery html css animation


【解决方案1】:

很遗憾,您无法使用 CSS 完成此操作。但是,slideToggle 确实有一个回调,您可以使用它来随着它的进行调整大小

$("#box").slideToggle(
        { duration: "fast", 
         progress: function() {
            $('#rightbot').height(
               $('#wrap').height()
               - $('#righttop').height()
               - 15 /* margins */)
       }
  });

JSFiddle

唯一难看的就是页边距。不过,可能也可以通过编程方式找到它们。

这是实现蓝框平滑调整大小的唯一方法

【讨论】:

    【解决方案2】:
    $("#click").on("click", function (e) {
        $("#box").slideToggle("fast");
        $("#rightbot").height($("#left").height() - $("#rightbot").height());
    });
    

    我使用#left div 的高度作为 100% 的值。

    另一种方法是使用overflow:hidden 包装div。

    解决方案取决于您的用例。

    【讨论】:

      【解决方案3】:

      怎么样:

        $("#click").on("click", function (e) {
          var height = "63%",
              $box = $("#box"),
              $blueBox = $("#rightbot");
      
          if($box.is(":visible")){           
             $box.hide("fast");
             $blueBox.height(height);
             return;
          }
      
          height = "43%";
          $box.show("fast");
          $blueBox.height(height);
      });
      

      在这里工作小提琴:http://jsfiddle.net/ncrqb/15/

      希望对你有帮助。

      【讨论】:

        【解决方案4】:
        $("#click").on("click", function (e) {
            $("#box").slideToggle("fast");
            if($("#rightbot").height()==300)
               $("#rightbot").height("43%");
            else
               $("#rightbot").height("300");
        });
        

        这里是fiddle

        【讨论】:

          【解决方案5】:

          以下代码将解决问题:-

          $("#click").on("click", function (e) {
                  $("#box").slideToggle("fast",function(){
                      var bluedivHeight =   300;//$("#rightbot").height() if want to have dynamic      
                      var toggleBoxHeight = $("#box").height();
                      if($("#box").css("display")==="block"){
                          $("#rightbot").height(bluedivHeight-toggleBoxHeight);
                      } else {
                          $("#rightbot").height(bluedivHeight);
                      }
                  });
              });
          

          【讨论】:

            【解决方案6】:

            http://jsfiddle.net/ncrqb/21/

            function centerBlue(){
                    var blue = $("#rightbot");
                    var parent = $("#left");
                    var rightTop = $("#righttop");
                    var left_space = (parent.outerHeight() - rightTop.outerHeight()) - 5;
                    blue.css("height",left_space+"px");
                    console.log(rightTop.outerHeight());
                }
            

            这是解决方案,它有效。

            所有这些答案都是对的,只是他们忘记了你需要在动画完成后重新调整蓝色框的大小,否则jQuery会返回之前的幻灯片状态高度。

            这是因为,jQuery 使用 window.setTimeout();这就是为什么它不会阻塞代码,只完成动画。

            【讨论】:

              猜你喜欢
              • 2021-03-24
              • 1970-01-01
              • 1970-01-01
              • 2013-02-02
              • 2013-07-27
              • 2019-06-13
              • 2014-11-29
              • 2013-01-22
              • 2022-08-17
              相关资源
              最近更新 更多