flex 上下固定中间滚动布局

flex上下固定中间滚动布局
传统 pc 端中,子容器高度超出父容器高度,通常使用 overflow:auto 可出现滚动条拖动显示溢出的内容,而移动web开发中,由于浏览器厂商的系统不同、版本不同,导致有部分机型不支持对弹性滚动,从而在开发中制造了所谓的 BUG。

上图如果在PC端中,我们可以利用 position:fixed 和 overflow:auto 进行简单的布局实现我们需要的效果,而在手机端遇到的问题如下:

ios4 和 android2.2 以下不支持 position:fixed
ios 和 android2.3 以下不支持 overflow:auto
ios4 和 android 不支持 overflow-scrolling
最严重的结果是:滚动区域内容无法拖动

对于 ios4 和 android2.2 以下不支持 position:fixed 的问题,有2种布局方法可以替代。
布局一: 定义页面整体高度为100%,然后使用 position:absolute 布局可解决

复制代码
/*

header
弹性滚动区域
*/ html,body{height:100%;} .wrap{width:100%;} .header,.footer{height:40px;line-height:40px;background-color:#D8D8D8;text-align:center;} .header{position: absolute;top:0;left:0;width:100%;} .footer{position: absolute;bottom:0;left:0;width:100%;} .main{position:absolute;z-index:1;top:40px;left:0;bottom:40px;width:100%;} 复制代码 复制代码

布局二: 定义页面整体高度为100%,然后使用 display:flex 布局可解决

复制代码
复制代码
/*

header
弹性滚动区域
*/ html,body{height:100%;} .wrap{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-box-orient:vertical;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;width:100%;height:100%;} .header,.footer{height:40px;line-height:40px;background-color:#D8D8D8;text-align:center;} .main{-webkit-box-flex:1;-webkit-flex:1;-ms-flex:1;flex:1;width:100%;} 复制代码 复制代码

那么剩下的主要问题是子容器高度超出父容器高度,子容器内容如何弹性滚动。
对于如何使用弹性滚动,这里并没有最好的方案,具体看产品的用户群、产品的定位等,简单介绍下:

方案一: overflow:auto和-webkit-overflow-scrolling: touch

适合场景:产品的用户群大多为 ios5+、android4+ 用户,建议采用 overflow-scrolling 做差异化体验,毕竟 iscroll4.js 在 android 机器下体验不顺畅,另外还加载了 10K 多的 js 代码。

overflow:auto 写法在 winphone8 和 android4+ 上有用。ios5+ 版本增加了一个新的属性:overflow-scrolling 这个属性可以激活平滑滚动,效果不错。

.css{
overflow:auto;/* winphone8和android4+ /
-webkit-overflow-scrolling: touch; /
ios5+ */
}

实现 bottom-wrap 区域滚动

.box-wrap {
display: flex;
flex-direction: column;
height: 100vh; — 可视区域
}
.top-wrap {
flex: 0 0 wrap;
}
.bottom-wrap {
flex: 1;
overflow-
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章