记录一下移动开发过程当中出现的问题。
从最多见的布局提及,固定头部或底部算是最多见的需求了
假定页面布局以下:css
<body> <div class="header"></div> <div class="main"></div> <div class="footer"></div> </body>
实现头部、底部固定,中间滚动,有三种简单实现方式:html
先从最简单的fixed布局开始,实现方式以下:ios
html, body { overflow: hidden; height: 100%; } .header, .footer { position: fixed; left: 0; height: 50px; } .header { top: 0; } .footer { bottom: 0; } .main { height: 100%; padding: 50px 0; }
这种布局在大多数状况下是正常显示的,但在移动端上(iOS)position: fixed
失效,会有所谓的兼容性问题;web
第二种方式absolute实现以下:工具
html, body { position: relative; height: 100%; } .header, .footer { position: absolute; left: 0; width: 100%; height: 50px; } .header { top: 0; } .footer { bottom: 0; } .main { height: 100%; width: 100%; padding: 50px 0; overflow: auto; }
这种方式在移动端(iOS)上能准确布局布局
第三种方式flex布局以下:flex
body { height: 100%; display: flex; flex-direction: column; } .header, .footer { height: 50px; } .main { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; /*ios滚动流畅*/ }
flex 定位在移动端兼容到了 iOS 7.1+,Android 4.4+,在iOS3.2~ios6.0可兼容flexbox,若是使用 autoprefixer 等工具还能够降级为旧版本的 flexbox ,能够兼容到 iOS 3.2 和 Android 2.1。flexbox
如果涉及到移动开发布局中碰到固定某一部分,其他部分可滚动,尽可能不要使用position: fixed
,可用absolute
替代,如果不须要考虑兼容性,用flex更佳。code