Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Prevent body scrolling when the user scrolls on fixed position div Reply To: Prevent body scrolling when the user scrolls on fixed position div

#266962
willmcpo
Participant

I wrote an npm package (body-scroll-lock) to solve this problem for all devices including iOS. Basically, it locks body scroll without locking the scrolling of a target element. It uses CSS for non iOS devices, and JS for iOS devices. It also avoids the troubles of using a position: fixed container. Works on desktop browsers too.

https://www.npmjs.com/package/body-scroll-lock

Here’s a demo

For more details on the rationale of this package, check out: https://medium.com/jsdownunder/locking-body-scroll-for-all-devices-22def9615177.

Give the package a try – if it works for you, please star the Github repo =)