Skip to content

[实习笔记]iOS中position:fixed吸底时的滑动出现抖动的解决方案 #17

@sunshinelover

Description

@sunshinelover

两种抖动

为什么抖动还会有两种?

其实是我碰到过两种抖动的场景,第一个场景是native的抖动,第二个场景是h5的抖动。

native的抖动

前端开发人员会在app中打开webview,这个时候iOS中position:fixed吸底时的滑动出现抖动应该是native造成的抖动,整个viewport跟着动,所以可以在生成schema的时候将参数bounce_disable设置为1禁止native的弹性效果,然后加上h5的这个效果,-webkit-overflow-scrolling 属性可以帮我们实现这个效果,它控制元素在移动设备上是否使用滚动回弹效果。

h5的抖动

方案一

//我是吸顶头部
.header{
    width:100%;
    height:50px;
    position:fixed;
    top:0px;
}
//我是中间要滑动的部分
.main{
    width:100%;
    height:auto;
    position:absolute;
    padding-top:50px;
    padding-bottom:50px;
    box-sizing:border-box;
    overflow-y:scroll;
    -webkit-overflow-scrolling :touch
}
//我是吸底尾部
.footer{
    width:100%;
    height:50px;
    position:fixed;
    bottom:0px;
}

解释:滑动部分overflow-y:scroll;所以在上下方向超出一屏的部分会变成滚动模式并且不溢出,然后这边吸顶和吸底设置的高度都是50,所以对应的中间滑动部分分别有padding-top:50px;和padding-bottom:50px;设置box-sizing:border-box;所以padding的增加不会增加.main的高度。

方案二

transform: translateZ(0);
-webkit-transform: translateZ(0);

解释:在使用position:fixed的元素上加上该属性。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions