阻止在父级创建滚动(在纯CSS中)弹出

问题描述:

我想创建一个简单的弹出窗口(想想按钮下方的组合框列表)。弹出窗口应显示在现有元素上,即不是预先分配空间。阻止在父级创建滚动(在纯CSS中)弹出

有很多例子使用`position:relative - > overflow:hidden - > position:absolute'层次结构,直到popover从容器底部流出,这种情况下,它的大小影响父容器并创建滚动。

这是codepen sample。我试图得到的是在最右边的列上没有滚动。

<div class="main"> 
    <div class="column"> 
    <h2>no overflow</h2> 
    <div class="tall">x</div> 
    <div class="tall">x</div> 
    </div> 
    <div class="column"> 
    <h2>basic overflow</h2> 
    <div class="tall">x</div> 
    <div class="tall">x</div> 
    <div class="tall">x</div> 
    </div> 
    <div class="column"> 
    <h2>popover without overflow</h2> 
    <div class="tall">x</div> 
    <div class="overlay"> 
     popover should be vislble below 
     <div class="overflow"> 
     <div class="absolute short"> 
      z 
     </div> 
     </div> 
    </div> 
    <div class="tall">x</div> 
    </div> 
    <div class="column"> 
    <h2>popover with overflow</h2> 
    <div class="tall">x</div> 
    <div class="overlay"> 
     popover should be vislble below 
     <div class="overflow"> 
     <div class="absolute"> 
      z 
     </div> 
     </div> 
    </div> 
    <div class="tall">x</div> 
    </div> 
</div> 

和SCSS文件:

html, body { 
    height: 95%; 

} 

.main { 
    display: flex; 
    flex-direction: row; 
    border: 2px solid red; 
    height: 100%; 
    .column { 
    margin-left: 20px; 
    height: 100%; 
    flex: 1; 
    border: 1px solid black; 
    overflow-y: auto; 
    .tall { 
     height: 300px; 
     border: 1px solid blue; 
    } 
    .overlay { 
     position: relative; 
     .overflow { 
     background: #F00; 
     overflow: hidden; 
     .absolute { 
      opacity: 0.5; 
      width: 100%; 
      height: 600px; 
      position: absolute; 
      background: #FF0; 
      &.short { 
      height: 200px; 
      } 
     } 
     } 
    } 
    } 
} 
+0

你有没有尝试去做到这一点?如果是这样,请分享您的代码。 –

+0

如果弹出窗口大于容器,为什么你不想创建一个滚动?否则,它的某些部分将被隐藏。 – mikepa88

+0

usign position:绝对值和负值不会影响父容器 –

我认为(如果我对这个问题清楚),这个问题可以很容易解决,只需用min-height替换你的高大类的heightmax-height如果将最小高度设置为短高度,则弹出窗口不应溢出。

我做了我自己的codepen做演示(我调整了popover的高度为100,并将最小高度设置为100)。代码与您发布的代码相同,但.short课程中的高度和.tall课程中的最小/最大高度除外。如果弹出窗口中的内容高度大于指定的高度,我会在短期课程中添加overflow:hidden;

我注释掉列没有popovers但随意评论他们进来。

希望这有助于

+0

Thx @Rachel,请参阅左列还是右列?右列仍然添加一个滚动条,而右列在到达列末端之前截断该弹出框。我倾向于使用柔性和响应式设计,并倾向于避免采取物理措施。 – Meir

+0

左边是我调整的那个 - 短的那个。您可以根据需要进行调整。为了比较的目的,我保留了正确的一个。你可以将左边的一个调整到最大高度:或者(更大)或者自动调整,但是你应该知道这个弹出窗口会有多大,再次看一下 –

+0

Thx。它很好地工作,正如你所指出的,我需要事先知道尺寸。我可能会继续搜索基于流(flex)的解决方案 – Meir