jquery - 扩展高度时div溢出并覆盖下一个div

标签 jquery html css

我在一个页面上有几个 div,每个都有一个“查看更多详细信息”链接。单击时,div 会展开并且应该毫无问题地填充父 div 的高度。然而,每个 div 溢出到下一个父 div 直到我滚动,然后它调整。如何使 div 增长而不会溢出?

现在发生了什么

我想要发生的事情

$(function() {
  var minHeight = null;
  var expCb = function($el) {
    $el.addClass('expanded');
    $el.find('.expand-details').text('Details -');
  };
  var collapseCb = function($el) {
    $el.removeClass('expanded');
    $el.find('.expand-details').text('Details +');
  };
  $('.details-content a.expand-details').click(function() {
    var currentCb = $(this).parent().hasClass('expanded') ? collapseCb : expCb;
    currentCb($(this).parent());
    updateCardDetailsLockups();
    return false;
  });
});
.details-content {
  max-height: 18px;
  overflow-y: hidden;
}

.details-content.expanded {
  max-height: 2000px;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent1">
  <div class="child col1">
    <div class="details-content">
      <a href="#" class="details-header expand-details">details +</a> ...
    </div>
  </div>
  <div class="child col2">
    ...
  </div>
</div>

<div class="parent2">
  <div class="child col1">
    <div class="details-content">
      <a href="#" class="details-header expand-details">details +</a> ...
    </div>
  </div>
  <div class="child col2">
    ...
  </div>
</div>

最佳答案

我认为我们需要更多信息来帮助您,但我怀疑您的 .col1.col2 类有它们的 float 属性放。您需要申请 clearfix给他们的 parent 扩大它以适应。

.details-content {
  max-height: 18px;
  overflow-y: hidden;
}

.details-content.expanded {
  max-height: 2000px;
  display:block;
}

.parent1 { border: 4px solid orangered; /* for visibility */ }
.parent2 { border: 4px solid teal; /* for visibility */ }
.col1 { float: left; /* assuming this is the case */ }

/* Here is the clearfix, apply it to your container */
.with-clearfix:after {
  content: "";
  display: table;
  clear: both;
}
<div class="parent1 with-clearfix">
    <div class="child col1">
        <div class="details-content expanded">
            <p>With clearfix, the parent container is sized based on its floating children. You can see the red-orange border surrounding the content.</p>
        </div>
    </div>
</div>
<div class="parent2 without-clearfix">
    <div class="child col1">
        <div class="details-content expanded">
            <p>Without clearfix, the teal border is collapsed and the children are spilling out of the parent. A clearfix is needed to size the parent to its children.</p>
        </div>
    </div>
</div>

关于jquery - 扩展高度时div溢出并覆盖下一个div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44554925/

相关文章:

javascript - 通过单击按钮切换 BODY 元素的背景颜色(CSS 类)时如何更改 H2 颜色?

css - 页脚位置 - 底部和中心

php - 基于 WooCommerce 中所选品牌的动态选择字段选项

jquery - jQuery livequery 插件的替代品?

javascript - 检查 cookie 是否一般存在,而不是特定的 cookie

jquery - 使用 jquery 创建 json 数组和 json 项?

jquery - 将 Prev 和 Next 按钮调整到表格的 Angular 落

html - 如何隐藏来自 wordpress 表格插件的 "Edit"选项?

php - 我的网站无法在 IE 中打开

javascript - 我将数据表用于 JSP 文件中填充的列表,而在 jsp 文件中列表显示不正确