A
A
Adam Sulumov2018-03-03 21:31:22
css
Adam Sulumov, 2018-03-03 21:31:22

How to prevent body from scrolling when opening a modal window without scrolling to the top of the page?

Hello,
There was a task to prevent the body from scrolling when opening a modal window. After digging around on the web, I found a solution to add " overflow: hidden " styles to the body , but in this case the page scrolls to the top, and I need it to stay in its position and just not scroll. Help solve the problem in the simplest way possible. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
wiyod, 2018-03-03
@wiyod

There is no beautiful and universal solution. There are two options, the first one will increase the page space due to the disappeared scroll, which in itself is not correct (well, you’ll get tired of looking for all elements that move down with a fixed position), and the second, correct option is possible only with a throw of the page up (this can be fixed by calculating each time the distance is scrolled and add top, but it's EVERYTHING'S UGLY).
I scored and do with scrolling. To do without scrolling is not difficult at all, but it's dirt.

D
Deodatuss, 2018-03-04
@Deodatuss

can somehow help, but the code is very ancient:
tyts
in short, then handle the scroll event and call preventDefault
UPDATE: I
thought it would become clear from the first example, here is the second:
tyts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question