【问题标题】:Flex Layout with fixed position (no scrolling) sidebar具有固定位置(无滚动)侧边栏的 Flex 布局
【发布时间】:2015-10-21 18:08:07
【问题描述】:

我有一个带有左右画布侧边栏的布局,将主要内容区域包围在中间。

侧边栏和主要内容是弹性项目,在弹性布局中从左到右定位。

侧边栏包含菜单和元链接。

我的问题是:滚动内容区域时,是否可以将侧边栏保持在固定位置,使它们保持在顶部而不向下滚动?

JS 小提琴:http://jsfiddle.net/Windwalker/gfozfpa6/2/

HTML:

<div class="flexcontainer">
    <div class="flexitem" id="canvas-left">
        <p>This content should not scroll</p>
    </div>
    <div class="flexitem" id="content">
        <div>
            <p>Scrolling Content</p>
        </div>
    </div>
    <div class="flexitem" id="canvas-right">
        <p>This content should not scroll</p>
    </div>
</div>

CSS:

.flexcontainer {
    display: flex;
    flex-direction: row;
    min-height: 100%;
    align-items: stretch;
}
.flexitem {
    display: flex;
}
#canvas-left {
    background: yellow;
    order: -1;
    flex: 0 0 57px;
}
#content {
    background: green;
    order: 1;
    padding: 1rem;
}
#content div {
    display: block;
}
#canvas-right{
    background: blue;
    order: 2;
    flex: 0 0 57px;
}

【问题讨论】:

  • 尽管如此,我不这么认为。一旦您开始添加 position 属性(如 absolute 或 fixed),您将删除块格式化上下文,因此 flexbox 将不再适用(AFAIK)。 也许有 align-self 的东西,但没有看到任何代码或结构很难评论。

标签: html css flexbox


【解决方案1】:

请使用提供的解决方案查看类似问题:How to simulate 'position:fixed' behavior on Flexbox-aligned sidebar。

根据您的代码,您还可以将内部内容包装在“位置:固定”包装器中:

<div class="flexitem" id="canvas-left">
    <div class="fixed">
        <p>This content should not scroll</p>
    </div>
</div>

并在 CSS 中添加适当的样式:

.fixed {
    position: fixed;
    width: 57px; /* according to #canvas-left */
}

以下是您的代码示例,其中包含固定的左侧边栏:http://jsfiddle.net/8hm3849m/。请注意,此技巧不会为您提供适当的侧边栏灵活网格,包装器的宽度应固定(或通过 JavaScript 动态设置)。

【讨论】:

  • 非常感谢这个解决方案! :-)
  • jsfiddle 拯救了我的一天,非常感谢! - 2019 年 6 月仍然有效
【解决方案2】:

这个问题很老,但我用

解决了一个类似的问题
position: sticky;
top: 0;

用于左右项目。 我也删除了

display: flex 

弹性项目的css,我认为没有必要。

https://jsfiddle.net/8mpxev0u/

【讨论】:

    【解决方案3】:

    我不知道如何使用 flex,但这里有一个更简单/替代的 css 删除所有那些 flex...并尝试永远不要向外部 div 添加填充,它在内部项目中更容易,那么你不需要计算如果有很多 div

    .flexcontainer {
        display: block;
        min-height: 100%;
        align-items: stretch;
    }
    .flexitem {
        display: flex;
    }
    #canvas-left {
        background: yellow;
        order: -1;
        left: 0px;
        width: 20%;
        position: fixed;
    }
    #content {
        position: absolute;
        background: green;
        order: 1;
        width: 60%;
        left: 20%;
    }
    #content p {
        display: block;
        padding: 1rem;
    }
    #canvas-right{
        background: blue;
        order: 2;
        right: 0px;
        width: 20%;
        position: fixed;
    }
    

    【讨论】:

    • 抱歉,我敲回车键太早了。这里是我对你的提议的真正想法:你的方法说明了我最初选择 flex 布局技术的原因:左列是一个带有图标的导航菜单,可以折叠和展开,即左列的宽度发生变化,内容列应该始终与左侧画布列对齐,无论其实际宽度如何。
    • 当然,我可以通过添加诸如menu-expanded 或menu-collapsed 之类的CSS 类来将20% 偏移量换成另一个值来管理这个问题,但这是一种硬编码..... :- /
    • 哦,那你是对的......左边栏有图标+扩展......所以你需要一个可拖动的固定“左边”栏? position:absolute; content div max/min-width......
    猜你喜欢
    • 1970-01-01
    • 2019-07-08
    • 2012-04-17
    • 2018-02-17
    • 2013-01-21
    • 2022-11-23
    • 2013-07-29
    • 2015-09-23
    • 1970-01-01
    相关资源
    最近更新 更多