Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery基于父div height动态更改子div height_Jquery_Html - Fatal编程技术网

使用jquery基于父div height动态更改子div height

使用jquery基于父div height动态更改子div height,jquery,html,Jquery,Html,我无法根据父项的高度动态更改子项的高度 这是一个例子 <div id="parent" style="height:600px"> <div id="children"></div> </div> 父级具有固定高度,但子级使用jQueryslideUp()和slideDown()函数更改其高度 如果子项高度大于父项,我需要将子项的高度设置为父项。另外,当我在children中滑动时,如果children在slideUp()之后的高度低于父级高度,

我无法根据父项的高度动态更改子项的高度

这是一个例子

<div id="parent" style="height:600px">
<div id="children"></div>
</div>
父级
具有固定高度,但子级
使用jQuery
slideUp()
slideDown()
函数更改其高度

如果子项
高度大于父项,我需要将子项
的高度设置为父项
。另外,当我在children
中滑动时,如果children
slideUp()之后的高度低于父级
高度,我需要将children
的高度设置为当前高度


提前感谢。

您可以尝试使用以下小提琴,看看结果是否符合预期(单击带下划线的文本查看t):

请注意,slideUp()首先将元素的高度降低为0并将其隐藏,然后可以在幻灯片结束时看到闪烁。您可能需要使用animate()并更改元素的高度:

$('#children p').on('click', function() {

    var cHeight = $('#children').height() + 'px';

    if ($('#children').height() > $("#parent").height() - 50) { 
         cHeight = ($("#parent").height() - 50) + 'px'; 
    } 

    $('#children').animate({'height' : cHeight});       

});

你试过什么吗?你可以做一个评论吗?请编辑你的问题,而不是发表评论。
$('#children p').on('click', function() {

    var cHeight = $('#children').height() + 'px';

    if ($('#children').height() > $("#parent").height() - 50) { 
         cHeight = ($("#parent").height() - 50) + 'px'; 
    } 

    $('#children').animate({'height' : cHeight});       

});