简体   繁体   中英

Disable/enable scroll only on Modal

I have a page with modal window, I want to in order to user could scroll divs in modal (when modal is open), but not all page, and if user close modal window he can scroll all page.

$(window).scroll(function (e) {
   if (!  modal ) {
       e.preventDefault();
   }
})

Is it possible ?

You can set body overflow property to hidden to prevent scroll. For example by adding some class to body element when popup is open:

.no-scroll {
    overflow: hidden;
}

Compare scroll behavior with overflow: hidden and auto in next demo.

Demo: http://jsfiddle.net/mrceaqxn/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM