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

后端 未结 20 1497
抹茶落季
抹茶落季 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 01:58

    As additional to @Karthick Kumar answer from bootstrap docs

    show is triggered at the start of an event

    shown is triggered on the completion of an action

    ... so it should be:

    $('.modal')
        .on('show.bs.modal', function (){
                $('body').css('overflow', 'hidden');
            })
        .on('hide.bs.modal', function (){
                // Also if you are using multiple modals (cascade) - additional check
                if ($('.modal.in').length == 1) {
                    $('body').css('overflow', 'auto');
                }
            });
    

提交回复
热议问题