Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modal causing double scroll Bar

Tags:

html

css

Opening a modal on my site is causing a double scroll bar (both vertical) issue. The modal uses position: fixed and overflow: auto. I know it is overflow: auto causing the issue, but I need this style, as my modal contains a lot of content which in most cases could not fit in the users viewport, therefore scrolling the content is a requirement.

.enquire-nav {
    font-family: inherit;
    top: 0;
    left: 0;
    z-index: 999;
    position: fixed;
    width: 100%;
    height: 100%;
    overflow: auto;
    background: rgba(36, 32, 33, .97);
}

Thanks in advance :]

like image 358
Leon Burman Avatar asked Jun 17 '16 15:06

Leon Burman


People also ask

Why do I have 2 scroll bars?

You're basically getting a double scrollbar because your giving the body min-height of 100vh AND setting an overflow. It appears this was done to keep the menu in the correct position on mobile devices. That fixed things in Chrome, I assume other browsers as well but I didn't do any heavy testing.

How do you stop body scroll when modal is open?

Approach: A simple solution to this problem is to set the value of the “overflow” property of the body element to “hidden” whenever the modal is opened, which disables the scroll on the selected element.

How do I make my scroll bar only for modal body?

Bootstrap 4.3 added new built-in scroll feature to modals. This makes only the modal-body content scroll if the size of the content would otherwise make the page scroll. To use it, just add the class modal-dialog-scrollable to the same div that has the modal-dialog class. Save this answer.

Should a modal be scrollable?

Modals should not scroll along their parents (namely html and body elements in most cases). Only content inside the modal should be scrollable if required.


1 Answers

In the end I found that the best solution was to toggle a class on the body when the modal is opened, this class would add overflow-y: hidden. This allows me to scroll the content of the modal if it overflows the body, but doesn't allow scrolling of the body itself at the same time. No more double scroll bars.

The jQuery:

  $(".menu-trigger, .primary-nav-item, .hamburger-one").click(function () {
    $(".mobilenav").fadeToggle(500);
    $(".top-menu").toggleClass("top-animate");
    $(".mid-menu").toggleClass("mid-animate");
    $(".bottom-menu").toggleClass("bottom-animate");
    $('body').toggleClass("no-scroll");

  });

The CSS:

.no-scroll {
    overflow-y: hidden;
}
like image 200
Leon Burman Avatar answered Oct 16 '22 06:10

Leon Burman