jquery - 动态改变div的高度到父级

标签 jquery html css animation

我怎样才能实现,蓝色框填充 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");
});

非常感谢!

最佳答案

不幸的是,您无法使用 CSS 完成此操作。但是,slideToggle 确实有一个回调,您可以使用它来调整事物的大小

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

并且在 JSFiddle

唯一丑陋的是边距。不过,也可能以编程方式找到它们。

这是唯一可以平滑调整蓝框大小的方法

关于jquery - 动态改变div的高度到父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18272684/

相关文章:

javascript - css样式根据动态内容设置div else的最小宽度

html - 为什么我的 &lt;input&gt; 元素的内容区域超出其父 <span> 元素?

jquery - 在 jquery ajax 中从 JS 端的 JsonResult 获取属性

jquery - html 和 css - 多级元素显示

html - id 不适用于移动浏览器的 HTML 元素

html - 如何在响应式网站中将菜单居中? (例子)

html - 从 FontAwesome 更改图标的颜色?

html - 行中的 Bootstrap 嵌套列与上一行中的列不对齐

html - 着色 CSS 半圆

javascript - jQuery $.each 循环中的 'each' 是什么?