我有一个左右画布侧边栏的布局,中间包含主要内容区域.
侧边栏和主要内容是弹性项目,从左到右以柔性布局定位.
侧边栏包含菜单和元链接.
我的问题是:滚动内容区域时,是否可以将侧边栏保持在固定位置,这样它们可以保持在最高位置而不会向下滚动?
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>
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
use*_*508 12
请查看提供解决方案的类似问题:如何在Flexbox对齐的侧边栏上模拟"位置:固定"行为.
根据您的代码,您还可以将内部内容包装在"position:fixed"包装器中:
<div class="flexitem" id="canvas-left">
<div class="fixed">
<p>This content should not scroll</p>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
并在CSS中添加适当的样式:
.fixed {
position: fixed;
width: 57px; /* according to #canvas-left */
}
Run Code Online (Sandbox Code Playgroud)
以下是带左侧边栏固定代码的示例:http://jsfiddle.net/8hm3849m/.请注意,此技巧不会为侧边栏提供适当的灵活网格,应修复包装器的宽度(或通过JavaScript动态设置).