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

后端 未结 20 1503
抹茶落季
抹茶落季 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:05

    If you use jQuery you can do this with scrollTop

    1. Save the body's vertical scroll position;
    2. Disable scroll on the body;
    3. Show modal;
    4. Close modal;
    5. Reenable scroll on body;
    6. Set saved scroll position.

    #modal {
        bottom: 0;
        position: fixed;
        overflow-y: scroll;
        overflow-x: hidden;
        top: 0;
        width: 100%;
    }

    $('.open-modal').click(function (e) {
        e.preventDefault();
        $('#modal').toggle();
        scrollTo = $('body').scrollTop();
        $('body').css("position", "fixed");
    });
    
    $('.close-modal').click(function (e) {
        e.preventDefault();
        $('#modal').toggle();
        $('body').css("position", "static");
        $('body').animate({scrollTop: scrollTo}, 0);
    });

提交回复
热议问题