How to prevent background scrolling when Bootstrap 3 modal open on mobile browsers?

后端 未结 20 1499
抹茶落季
抹茶落季 2020-12-01 01:45

How to prevent background scrolling when Bootstrap 3 modal open on mobile platforms? On desktop browsers the background is prevented from scrolling and works as it should.<

20条回答
  •  情书的邮戳
    2020-12-01 02:02

    Had an issue with this as well, iPhone + Safari where needed to add:

    position: fixed;
    

    As mentioned elsewhere, this created a scroll-to-top issue. Fix that worked for me was to capture the position to top upon modal open, and then animate to that position on modal close

    upon modal open:

    scrollTo = $('body').scrollTop();
    $('body').css("position", "fixed");
    

    upon modal close

    $('body').css("position", "static");
    $('body').animate({scrollTop: scrollTo}, 0);
    

提交回复
热议问题