如何让点击触发的Modal弹窗固定位置不随滚动移动?
解决Modal弹窗随页面滚动移动的问题
你的弹窗会跟着页面滚动移动,核心问题是.modal容器没有占满整个视口,且定位后的尺寸设置不符合需求,导致弹窗内容的固定定位没有正确参照视口。以下是具体修改方案:
关键CSS修改
1. 调整.modal容器样式
将.modal改为铺满整个视口,同时保留页面滚动能力:
.modal { display: none; position: fixed; z-index: 1; left: 0; top: 0; width: 100%; /* 占满视口宽度 */ height: 100%; /* 占满视口高度 */ overflow: visible; /* 允许页面滚动 */ background-color: rgba(0,0,0,0.5); /* 可选:添加半透明背景,增强视觉区分 */ }
2. 确认弹窗内容的固定定位
保留.modal-content的position: fixed,确保它始终固定在视口中央:
.modal-content { width: 70%; height: 80%; position: fixed; left: 0; right: 0; top: 0; bottom: 0; margin: auto; max-width: 100%; max-height: 100%; background-color: #0E0E0E; border: 2px solid #FFFFFF; }
修改后的完整代码
<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> /* The Modal (background) */ .modal { display: none; position: fixed; z-index: 1; left: 0; top: 0; width: 100%; height: 100%; overflow: visible; background-color: rgba(0,0,0,0.5); } /* Modal Content/Box */ .modal-content { width: 70%; height: 80%; position: fixed; left: 0; right: 0; top: 0; bottom: 0; margin: auto; max-width: 100%; max-height: 100%; background-color: #0E0E0E; border: 2px solid #FFFFFF; } /* The Close Button */ .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; position: relative; left: -10px; top: -1px; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; } @media only screen and (min-width: 600px) { .col-s-1 {width: 8.33%;} .col-s-2 {width: 16.66%;} .col-s-3 {width: 25%;} .col-s-4 {width: 33.33%;} .col-s-5 {width: 41.66%;} .col-s-6 {width: 50%;} .col-s-7 {width: 58.33%;} .col-s-8 {width: 66.66%;} .col-s-9 {width: 75%;} .col-s-10 {width: 83.33%;} .col-s-11 {width: 91.66%;} .col-s-12 {width: 100%;} } @media only screen and (min-width: 768px) { .col-1 {width: 8.33%;} .col-2 {width: 16.66%;} .col-3 {width: 25%;} .col-4 {width: 33.33%;} .col-5 {width: 41.66%;} .col-6 {width: 50%;} .col-7 {width: 58.33%;} .col-8 {width: 66.66%;} .col-9 {width: 75%;} .col-10 {width: 83.33%;} .col-11 {width: 91.66%;} .col-12 {width: 100%;} } .video{ width: 65%; height: 65%; position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; max-width: 100%; max-height: 100%; } .myBtn{ margin-top: 20%; } </style> </head> <body> <!-- The Modal --> <div id="myModal" class="modal"> <!-- Modal content --> <div class="modal-content"> <span class="close">×</span> <center> <div class="video"> <video src="https://cdn.shopify.com/videos/c/o/v/1ef399dd4bb345e485f6e4784a9d0031.mp4" type="video/mp4" autoplay width="100%" height="100%"></video> </center> </div> </div> <button class="myBtn" id="myBtn">Open Modal</button> </body> <script> // Get the modal var modal = document.getElementById("myModal"); var btn = document.getElementById("myBtn"); var span = document.getElementsByClassName("close")[0]; btn.onclick = function() { modal.style.display = "block"; } span.onclick = function() { modal.style.display = "none"; } window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } </script> </html>
修改后效果:弹窗会固定在视口中央,页面可以正常滚动,点击弹窗外部区域即可关闭弹窗。
内容的提问来源于stack exchange,提问作者Mellemo
相关产品推荐
相关产品推荐

