My goal is to prevent page scrolling when a modal is opened without causing the scrollbar to disappear or the content to shift abruptly. I want to freeze the scroll position I'm currently at, keep the scrollbar visible, and ensure that features like 'hide on scroll' on the header do not trigger under the modal's dark underlay. How can I achieve this without any unwanted side effects?
I've experimented with different methods, including applying the following styles directly to the <body>
:
position: fixed;
height: 100%;
width: 100%;
top: 0;
left: 0;
overflow-y: scroll;
While this approach prevents scrolling and keeps the scrollbar visible, using position: fixed;
causes the viewport to jump to the top when the modal is opened, which is not desirable.
In my Vue project, the template in my App.vue file is structured as follows:
<template>
<div>
<AppModal></AppModal>
<AppHeader></AppHeader>
<AppMain></AppMain>
<AppFooter></AppFooter>
</div>
</template>
I am searching for a concise solution that does not require using JavaScript.