项目中存在的问题: position:fixed
的弹窗显示时,背景的内容仍然可滚动?
以往的解决方案: 采取js
的方式将overflow:hidden
;
根本原因是: position:fixed
是相对根元素(html
),而页面的滚动条是overflow生成
解决方案: 弹窗时锁定背景不进行滚动,可设html
的overflow:hidden
,然后将滚动条给相对应的body
或整个大包裹的div
元素
<style>
html {
width: 100vw;
height: 100vh;
overflow: hidden;
}
body {
height: 100;
overflow: scroll;
}
.fix {
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: rgba(0, 0, 0, .48);
display: flex;
justify-content: center;
align-items: center;
text-align: center;
z-index: 9;
}
.fix>div {
width: 200px;
line-height: 50px;
background: #fff;
border-radius: 10px;
}
</style>
<body>
<div class='fix'>
<div>弹窗的显示内容</div>
</div>
</body>
以上的方法原理是:position: fixed
;是相对于根元素的定位,把根元素的滚动条去除,转而使用body或div的元素使用即可!