Bootstrap 3 collapse:获取已折叠元素的非折叠高度

Bootstrap 3 collapse: Get non-collapsed height of a collapsed element

本文关键字:折叠 元素 高度 collapse 获取 Bootstrap      更新时间:2023-09-26

我有一些引导面板,其中panel-heading用于折叠panel-body。我想知道当panel-body处于折叠状态时,是否可以访问其非折叠高度?

您可以查看这个jsFiddle,其中面板主体已经折叠。单击面板标题将展开主体。当身体塌陷时,如何在未塌陷(不展开)的情况下确定其高度?

这个问题是Bootstrap 3特有的,所以这个答案也是一样的,通常可能没有用处。

在引导程序3中,当一个元素被折叠时,其高度使用元素样式显式设置为0px。您可以通过以下方式禁用此内嵌元素样式并访问其高度:

$(element_selector)['height']('').height();

这将不会显示元素,因为除了将高度设置为0px之外,当元素折叠时,其display属性也将设置为none

您可以跟踪面板内有多少元素,然后从中计算他的高度:

$(document).ready(function(){
  var rowHeight = 50;
  var marginBottom = 50;
  var nrOfRows = 0;
  $('.my-table .row').each(function(){
     nrOfRows ++
  })
  var panelHeight = (rowHeight*nrOfRows)+prseInt(marginBottom)
})