Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable body scrolling when modal is open IOS only

IOS only / iPhone X / iPhone 7 etc.

Even jquery modal libraries don't work! https://jquerymodal.com/ - Open the modal on your iPhone and you will be able to scroll the body.

I am finding it very hard to find a solution that stops the body scrolling without making the page jump to the top each time the modal is opened (which is just as bad experience as the page scrolling)

It seems this is a massive problem lots of people experiencing this. As you can see here:

  • How to prevent body scrolling on iOS 12 when modal opened?
  • https://stackoverflow.com/questions/49760984/stopping-body-scroll-on-modal-open-bootstrap-4

I have hunted the internet with no luck, has anyone a solution?!

like image 342
BennKingy Avatar asked Feb 20 '19 12:02

BennKingy


1 Answers

I've created the following solution, which works on iOS 12!

Although the embedded demo below uses Bootstrap 4, the same solution works equally well with Bootstrap 3 since none of the modal class or event names are different.

Step 1: Use fixed positioning to freeze the body in place when the modal is open

When a Bootstrap modal is opened, a class called .modal-open is added to the body. Add the following additional styles to this class:

body {
    &.modal-open {
        bottom: 0;
        left: 0;
        position: fixed;
        right: 0;
        top: 0;
    }
}

Now whenever a modal is opened, the body will be fixed in place and sized to the same dimensions as the viewport itself. This completely prevents scrolling because there's nowhere and nothing to scroll to!

But: this also means that opening a modal will cause the page to jump to the top, because the body no longer extends past the bottom edge of the viewport (assuming the page content is taller).

Step 2: Simulate the previous scroll distance while the modal is open

Bootstrap exposes events that fire when a modal is opened or closed. We can use these to solve the "jump to the top" issue by pulling the top of the body up when a modal is opened, so that it looks like the scroll position hasn't changed:

$(function() {
    var $window = $(window),
        $body = $("body"),
        $modal = $(".modal"),
        scrollDistance = 0;

    $modal.on("show.bs.modal", function() {
        // Get the scroll distance at the time the modal was opened
        scrollDistance = $window.scrollTop();

        // Pull the top of the body up by that amount
        $body.css("top", scrollDistance * -1);
    });
});

However, the page will still jump to the top when the modal is closed because the scrollTop value of the window is still 0.

Step 3: Reset everything when the modal is closed

Now we just need to hook into the event that fires when the modal is closed and put everything back how it was:

  • Remove the fixed positioning and negative top value on the body
  • Set the window's scroll position back to what it was originally
$modal.on("hidden.bs.modal", function() {
    // Remove the negative top value on the body
    $body.css("top", "");

    // Set the window's scroll position back to what it was before the modal was opened
    $window.scrollTop(scrollDistance);  
});

There's no need to manually remove the fixed positioning on the body, because this is set through the .modal-open class, which Bootstrap removes when the modal is closed.


Demo

Put it all together, and now you can prevent background scrolling on iOS while a modal is open without losing your scroll position!

Open the following link on an iOS device: https://daguy.github.io/ios-modal-fix/

like image 84
daGUY Avatar answered Nov 02 '22 16:11

daGUY