头尾固定,中间区域可以滑动效果是移动端最常见的效果,以京东页面为例。以前开发时常用的方法是用固定布局
position:fixed;
实现,但是该方法在ios上或者其他机型上会出现问题。现在,可以用flex方法快速实现该布局。
html:
<body>
<div class="flex-wrap">
<div class="header">Header</div>
<div class="cont">
中间内容区域
</div>
<div class="footer">Footer</div>
</div>
</body>
css:
html,
body {
width: 100%;
height: 100%;
}
.flex-wrap {
display: flex;
flex-direction: column;
height: 100%;
}
.flex-wrap .header,
.flex-wrap .footer {
height: 40px;
background: lime;
}
.flex-wrap .cont {
flex: 1;
overflow: scroll;
background: red;
}
说明: css样式中,一定要设置html,body以及最外层包裹容器的高度为100%,同时中间内容区域的样式一定要添加flex:1;
flex是flex-grow、flex-shrink、flex-basis的缩写;
关于弹性布局的具体用法可以参考文章:
https://www.html.cn/archives/7236
http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html
如有更好方法,欢迎大家留言,文章中有不足之处,欢迎大家指出。