Prevent iOS bounce without disabling scroll ability

前端 未结 7 1260
情歌与酒
情歌与酒 2020-11-28 06:14

I am trying to implement a solution to prevent the iOS bounce effect in Safari for iOS when a web page content is larger than the viewport.

The page I am working on

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 06:57

    I went through a few answers on SO and things were looking bleak until stumbled upon this code.

    html {
      position: fixed;
      height: 100%;
      overflow: hidden;
    }
    
    body {
      width: 100vw;
      height: 100vh;
      overflow-y: scroll;
      overflow-x: hidden;
      -webkit-overflow-scrolling: touch;
    }
    

    The style declarations for body can be put on any element that you want to have the ability to scroll. You can also alter overflow-x and overflow-y as needed. I personally needed it to NOT scroll to the sides so I declared it as so.

    update Sept 15 2017: I had to use this fix for another project and I was able to do without these declarations position: fixed and height: 100%; on the html selector. YMMV

    Update April 12 2018 (mentioned in comments): If you're using fixed elements on the page, those elements may have a visual "shakiness" when scrolling.

提交回复
热议问题