モーダルが開いている場合、<body>
タグにはmodal-open
というクラスが与えられ、ページ自体のスクロールバーが無効になります。
モーダル自体は、ビューポート全体を埋めるように配置され、overflow: auto
に設定されます。つまり、モーダルの内容がビューポートより大きい場合、スクロールバーはモーダル要素にのみ表示されます。
/* when modal is closed: */
#body {
text-align: center; padding: 30px;
}
#body:not(.modal-open) {
overflow: auto;
}
#body:not(.modal-open) #modal {
display:none;
}
/* when modal is open: */
#body.modal-open {
overflow: hidden;
}
#body.modal-open #modal {
overflow: auto;
padding: 10px;
background: rgba(0,0,0,0.7);
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 999999;
}
#modal_inner {
width: 100px;
height: 200px;
background: #fff;
border: 1px solid #000;
text-align: center;
padding: 50px;
margin: auto;
}
<html>
<body id="body">
<div style="width: 200%; height: 300%">
<button onclick="document.getElementById('body').classList.toggle('modal-open')">open modal</button>
</div>
<div id="modal">
<div id="modal_inner">
This is the modal!
<button onclick="document.getElementById('body').classList.toggle('modal-open')">close modal</button>
</div>
</div>
</body>
</html>
'document.documentElement.style.overflow =「hidden'':
あなたは、以下の概念の最小限の証拠を見ることができます – Ryan