【问题标题】:Section height is 0 because of div's absolute position inside由于 div 内部的绝对位置,节高为 0
【发布时间】:2018-05-15 13:46:27
【问题描述】:

所以基本上我有一个section标签,里面有3个div,绝对定位(用于同位素插件)

              <section id="section1" class="cd-section"> // pos. static/relative has height 0px
                    <div class="itemIso"> // pos. absolute, height ~900px
                          div1
                    </div>
                    <div class="itemIso"> // pos. absolute, height ~700px
                          div2
                    </div>
                    <div class="itemIso"> // pos. absolute, height ~600px
                          div3
                    </div>
              </section>

这是一张图片:

现在显然我的问题是,由于 div 的位置是绝对的,而 section1 是静态/相对的,它的高度是 0,我希望它是 900+700+600=whatever,2200px?是吗? 我有 6 个这样的部分,我无法设置固定高度,因为它可能会发生变化。

任何 jQuery、js、任何可以解决这个问题的东西?

【问题讨论】:

  • 所以也许绝对位置不是你的朋友,你可以使用 display:inline-block; divs,也在你的例子中,你希望你的容器有什么高度? 600? 1100 ? 500?解释您的绝对元素是如何放置的,以便我们提供帮助;)
  • 我不是让它们绝对化的人,它是我正在使用的同位素网格 (isotope.metafizzy.co),它在某种程度上是我试图用它制作的投资组合。
  • 你已经在页面上加载了 js 并使用 $grid.isotope 进行了初始化?因为它不是你可以在没有 js 的情况下轻松复制的东西,isotope 会自动为你的 div 设置好的高度
  • 可以用 CSS 覆盖位置吗?
  • @themarksmaker 如果同位素库对这个问题至关重要,那么您应该为其添加标签。并扔掉 [height] 标签。

标签: html css position height absolute


【解决方案1】:

最好的解决方案是在每次改变孩子身高后重新计算截面的高度。使用这种方法,您需要在加载图像、字体或调整屏幕大小后调用calculateHeight() 函数。您还需要确保外部插件不会覆盖元素,例如示例的第 2 和第 3 部分。

function calculateHeight(){
  var maxHeight = 0;

  $(".section > div").each(function() {
    var height = $(this).height();   
    height += $(this).position().top;    
    
    if (height > maxHeight)
        maxHeight = height;
  });

  $('.section').height(maxHeight);
}


$(function() {
  
  calculateHeight();
  
  $(window).resize(calculateHeight);
});
.section{
  position: relative; 
  background: red;
}

.section-i{
  position :absolute;
  left: 0;
  width: 100%;
  background: rgba(0,0,0,0.5);
  
}

.section-i.s1{
  top: 15px;
  height: 100px;
}

.section-i.s2{
  top: 130px;
  height: 200px;
}

.section-i.s3{
  top: 300px;
  height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="section">
  <div class="section-i s1">
  </div>
  <div class="section-i s2">
  </div>
  <div class="section-i s3">
  </div>
</section>

希望能帮到你

【讨论】:

  • 很确定在您的示例部分中,类部分的高度为 0
  • 对不起,这个 $('.section').height(maxHeight) 是我的错误。我知道解决方案是电线,但检查最新放置元素的高度和顶部。
  • 问题是在第一次加载时它不能很好地工作,它像 ~100-150px 一样丢失?(部分变得像 2100px 高度),在我调整窗口大小后它会立即捕捉到点(2209px 高度) .知道为什么吗?
  • 好吧,我认为可以是项目内的元素,例如,图像或字体正在异步加载,然后当加载此资源时,它们会更改其容器的大小,实际上这可能是发生在第三个插件上。你能检查一下吗?
  • 你能创建一个小提琴吗?
【解决方案2】:

使用 JQuery...

请注意,此示例不考虑子 div 的位置,只考虑它们的高度。

$(function() {
  var sectionHeight = 0;

  $("#section1 > div").each(function() {
    sectionHeight += $(this).height();
  });

  $('#section1').height(sectionHeight);
});
.cd-section {
  position: relative;
  background: lightgrey;
  border: 5px solid red;
}

.cd-section>div {
  position: absolute;
  height: 200px;
  width: 100%;
  background: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="section1" class="cd-section">
  <div class="itemIso">
  </div>
  <div class="itemIso">
  </div>
  <div class="itemIso">
  </div>
</section>

【讨论】:

  • 是的,正是我想要的。微小的变化,它就像一个魅力。干杯伙伴!
  • 这种方法不关心元素的位置,这假设每个元素是一个接一个的,没有像margin或top这样的间距。你需要检查一下。
猜你喜欢
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-30
  • 1970-01-01
  • 2018-12-05
  • 2013-05-05
  • 1970-01-01
相关资源
最近更新 更多