精英干员瑕光 2022-05-11 23:47 采纳率: 76.5%
浏览 421
已结题

点击按钮弹出弹窗,并且背景变暗不可点击

希望实现的效果:
点击按钮后弹出弹窗(已实现),同时背景变暗,除了弹窗点击其他地方都不会有响应。
希望能得到一个简单的模板或者方法。
实践时遇到的问题:
采用显示隐藏的方法,不符合预期的想法,希望背景变暗但仍然能看到原页面而不是直接消失。
尝试用fixed放在顶层,但是显示不出来。

  • 写回答

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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月20日
  • 已采纳回答 5月12日
  • 创建了问题 5月11日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效