美文网首页
Web移动端Fixed布局的解决方案

Web移动端Fixed布局的解决方案

作者: 不落05 | 来源:发表于2017-08-11 14:11 被阅读0次

移动端业务开发,iOS 下经常会有 fixed 元素和输入框(input 元素)同时存在的情况。 但是 fixed 元素在有软键盘唤起的情况下,会出现许多莫名其妙的问题。 这篇文章里就提供一个简单的有输入框情况下的 fixed 布局方案。

iOS下的 Fixed + Input BUG现象

// html
<body class="layout-fixed">
    <!-- fixed定位的头部 -->
    <header>
    </header>
  
    <!-- 可以滚动的区域 -->
    <main>
        <!-- 内容在这里... -->
    </main>
    <!-- fixed定位的底部 -->
    <footer>
        <input type="text" placeholder="Footer..."/>
        <button class="submit">提交</button>
    </footer>
</body>
//  css如下
header, footer, main {
    display: block;
}

header {
    position: fixed;
    height: 50px;
    left: 0;
    right: 0;
    top: 0;
}

footer {
    position: fixed;
    height: 34px;
    left: 0;
    right: 0;
    bottom: 0;
}

main {
    margin-top: 50px;
    margin-bottom: 34px;
    height: 2000px
}

问题描述: 软键盘唤起后,页面的 fixed 元素将失效(即无法浮动,也可以理解为变成了 absolute 定位),所以当页面超过一屏且滚动时,失效的 fixed 元素就会跟随滚动了。
条件:

  • 页面使用原生滚动(body);

解决方案:

  • 使用iscroll等第三方库布局方案;
  • 不使用原生滚动(body);
// 不使用原生滚动(body);

header, footer, main {
    display: block;
}

header {
    position: fixed;
    height: 50px;
    left: 0;
    right: 0;
    top: 0;
}

footer {
    position: fixed;
    height: 34px;
    left: 0;
    right: 0;
    bottom: 0;
}

main {
    /* main绝对定位,进行内部滚动 */
    position: absolute;
    top: 50px;
    bottom: 34px;
    /* 使之可以滚动 */
    overflow-y: scroll;
    -webkit-overflow-scrolling: touch;
}

main .content {
    height: 2000px;
}

扩展阅读:
overflow-scrolling,will-change等属性会触发新的层叠上下文。
启用GPU硬件加速的属性。
transform对普通元素的N多渲染影响

  • 影响层叠上下文
  • 影响元素的fixed的跟随效果
  • 影响元素的参照物
  • z-index,opactiy值不为1,transform不是none,will-change

相关文章

网友评论

      本文标题:Web移动端Fixed布局的解决方案

      本文链接:https://www.haomeiwen.com/subject/uhsolxtx.html