热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何向flex项目的子项目添加滚动?-Howtoaddscrolltochildofflexitem?

Withaflexcontainerwithafixedheight,Icanmakeaflexitemscrollablebysettingoverflow:au

With a flex container with a fixed height, I can make a flex item scrollable by setting overflow: auto, like so:

有了一个固定高度的flex容器,我可以通过设置overflow: auto来实现可滚动项,如下所示:

page {
  display: flex;
  flex-flow: row wrap;
  height: 200px;
}
left-panel {
  flex: 1;
  display: block;
  background: lightblue;
}
//main {flex: 5; display: block;}
header {
  background: turquoise;
  display: block
}
//main-content-wrapper {flex: 4; display:flex; flex-direction: column}
right-panel {
  flex: 1;
  overflow: auto;
}
content-panel {
  background: pink;
  display: block;
}
content {
  height: 1000px;
  display: block;
}

  left-panel
    
(static)
header
(static) content
(scrolls)

fiddle

小提琴

But I would like to make a child of the flex item scroll instead.

但是我想要让一个flex项目滚动的孩子。

I would think that setting height: 100% on the flex item and overflow: auto on the child element I want to scroll would work, but it does not:

我认为在flex项目上设置高度为100%,而在我想滚动的子元素上设置overflow: auto就可以了,但它不行:

page {
  display: flex;
  flex-flow: row wrap;
  height: 200px;
}
left-panel {
  flex: 1;
  display: block;
  background: lightblue;
}
//main {flex: 5; display: block;}
header {
  background: turquoise;
  display: block
}
//main-content-wrapper {flex: 4; display:flex; flex-direction: column}
right-panel {
  flex: 1;
  height: 100%;
}
content-panel {
  background: pink;
  display: block;
  overflow: auto;
}
content {
  height: 1000px;
  display: block;
}

  left-panel
    
(static)
header
(static) content
(scrolls)

fiddle

小提琴

How can I make this work?

我怎样才能让它工作呢?

And why doesn't the second fiddle work?

为什么第二小提琴不奏效呢?

Added OP comments:

OP的评论:

  • I can see there are several related questions, but the ones I found are either not directly applicable, or mention workarounds to bugs that may have been fixed since.

    我可以看到有几个相关的问题,但是我发现的这些问题不是直接适用的,或者是对可能已经修复的bug提出解决办法。

  • I would like to avoid using explicit height calculations to reduce coupling of components.

    我希望避免使用显式的高度计算来减少组件之间的耦合。

  • I could convert the right-panel into a nested flexbox with flex-direction: column, but I was hoping to avoid that.

    我可以将右面板转换为带有flex-direction: column的嵌套flexbox,但我希望避免这种情况。

  • In case a flat structure (like in the second fiddle) is not possible without an explicit height calculation, it would be nice with an explanation of why. Could it be considered a bug with flexbox or am I misunderstanding something fundamental about css?

    如果一个平面结构(如第二小提琴)不可能没有显式的高度计算,它将很好地解释为什么。它会被认为是带有flexbox的bug吗?还是我误解了css的一些基本原理?

3 个解决方案

#1


4  

If you're applying overflow: auto to an element so it can launch a vertical scrollbar, then also give it a height limitation.

如果您正在应用溢出:auto到一个元素,那么它可以启动一个垂直滚动条,然后也给它一个高度限制。

In this case, you just need to add height: 100% to the element you want to make scrollable.

在这种情况下,您只需向要使可滚动的元素添加height: 100%。

content-panel {
  background: pink;
  display: block;
  overflow: auto;
  height: 100%; /* NEW */
}

Of course, you'll need to adjust the actual percentage value to subtract the height of the header. Maybe something like this:

当然,您需要调整实际的百分比值来减去header的高度。也许是这样的:

height: calc(100% - 80px)

OR, perhaps a better approach (and consistent with your added comments rejecting fixed heights), just make right-panel a column-direction flex container:

或者,也许有更好的方法(并与您所添加的拒绝固定高度的评论保持一致),将右面板设置为一个列方向的flex容器:

page {
  display: flex;
  flex-flow: row wrap;
  height: 200px;
}
left-panel {
  flex: 1;
  display: block;
  background: lightblue;
}
header {
  background: turquoise;
  display: block
}
right-panel {
  flex: 1;
  height: 100%;
  display: flex;          /* NEW */
  flex-direction: column; /* NEW */
}
content-panel {
  background: pink;
  overflow: auto;
}
content {
  height: 1000px;
  display: block;
}

  left-panel
    
(static)
header
(static) content
(scrolls)


EDIT (based on revised question)

With regard to the overflow property:

关于溢出性质:

The overflow property specifies whether to clip content, render scrollbars or just display content when it overflows its block level container.

overflow属性指定在内容溢出块级容器时是剪辑内容、呈现滚动条还是仅仅显示内容。

In order for the overflow property to have an effect, the block level container must either have a bounding height (height or max-height) or have white-space set to nowrap.

为了使溢出属性具有效果,块级容器必须具有一个边界高度(高度或最大高度)或设置为nowrap的空白。

https://developer.mozilla.org/en-US/docs/Web/CSS/overflow

https://developer.mozilla.org/en-US/docs/Web/CSS/overflow

#2


2  

Make your right-panel a column flexbox and add flex:1 to content-panel - see demo below:

将你的右面板设置为一个列flexbox,并在内容面板中添加flex:1——参见下面的演示:

page {
  display: flex;
  flex-flow: row wrap;
  height: 200px;
}
left-panel {
  flex: 1;
  display:block;
  background: lightblue;
}
header {
  background: turquoise;
  display:block;
}
right-panel {
  flex: 1;
  display: flex;/*Added this*/
  flex-direction: column;/*Added this*/
  height: 100%;
}
content-panel {
  background: pink;
  display:block;
  overflow: auto;
  flex:1; /*Added this*/
}
content {
  display: block;
  height: 1000px;
}

  left-panel
    
(static)
header
(static) content
(scrolls)
more content here
more content here
more content here
more content here
more content here
more content here
more content here
more content here
more content here

#3


0  

your content-panel already has height 1000px and your content has overflow:auto. The only thing you missed is you did not specify the content-panel height. content-panel height must <content height in order for it to display scroll bar

你的内容面板已经有了1000px的高度,你的内容已经溢出了:auto。您惟一遗漏的是没有指定内容面板的高度。内容面板高度必须小于内容高度才能显示滚动条


    
      content
      
(scrolls)

推荐阅读
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 标题: ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 本文详细介绍了Android中的坐标系以及与View相关的方法。首先介绍了Android坐标系和视图坐标系的概念,并通过图示进行了解释。接着提到了View的大小可以超过手机屏幕,并且只有在手机屏幕内才能看到。最后,作者表示将在后续文章中继续探讨与View相关的内容。 ... [详细]
  • 本文整理了Java中java.lang.NoSuchMethodError.getMessage()方法的一些代码示例,展示了NoSuchMethodErr ... [详细]
  • vb.net面试题,请大家帮忙,谢谢。如果需要讲详细一点,那就加我QQ531412815第4题,潜在的错误,这里的错误不是常规错误,属于那种只有在运行是才知道的错误:Catchex ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了brain的意思、读音、翻译、用法、发音、词组、同反义词等内容,以及脑新东方在线英语词典的相关信息。还包括了brain的词汇搭配、形容词和名词的用法,以及与brain相关的短语和词组。此外,还介绍了与brain相关的医学术语和智囊团等相关内容。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • Tkinter Frame容器grid布局并使用Scrollbar滚动原理
    本文介绍了如何使用Tkinter实现Frame容器的grid布局,并通过Scrollbar实现滚动效果。通过将Canvas作为父容器,使用滚动Canvas来滚动Frame,实现了在Frame中添加多个按钮,并通过Scrollbar进行滚动。同时,还介绍了更新Frame大小和绑定滚动按钮的方法,以及配置Scrollbar的相关参数。 ... [详细]
  • 涉及的知识点-ViewGroup的测量与布局-View的测量与布局-滑动冲突的处理-VelocityTracker滑动速率跟踪-Scroller实现弹性滑动-屏幕宽高的获取等实现步 ... [详细]
author-avatar
购物狂RZBZ_719
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有