希望实现的效果:
点击按钮后弹出弹窗(已实现),同时背景变暗,除了弹窗点击其他地方都不会有响应。
希望能得到一个简单的模板或者方法。
实践时遇到的问题:
采用显示隐藏的方法,不符合预期的想法,希望背景变暗但仍然能看到原页面而不是直接消失。
尝试用fixed放在顶层,但是显示不出来。
点击按钮弹出弹窗,并且背景变暗不可点击
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- ZionHH 2022-05-12 10:06关注
简单实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .modal-cover { width: 100%; height: 100%; position: fixed; top: 0; left: 0; background-color: rgba(0, 0, 0, .1); z-index: 99; display: none; } .modal { width: 40%; height: 300px; background-color: #fff; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } </style> </head> <body> <button onclick="showModal()">show modal</button> <div id="modal" class="modal-cover"> <div class="modal"> <button onclick="closeModal()">close modal</button> </div> </div> <script> var modal = document.getElementById('modal') function showModal() { modal.style.display = 'block' } function closeModal() { modal.style.display = 'none' } </script> </body> </html>
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用 2