【问题标题】:How to disable background when modal window pops up弹出模态窗口时如何禁用背景
【发布时间】:2018-01-18 08:45:16
【问题描述】:

我正在构建一个网页模板,并对模态弹出窗口进行了一些修改,如下所示:

<div class="modal fade" id="myModal4" tabindex="-1" role="dialog" style="background-color:#FECE1A;display:none;width:750px;left:46%" aria-hidden="true">
  <div class="modal-dialog">
  <script>
    $("#myModal4").on("show", function () {
      $("body").addClass("modal-open");
    }).on("hidden", function () {
      $("body").removeClass("modal-open")
    });
  </script>
  <!--Modal Content-->
  </div>
</div>

body.modal-open css 函数是这样的

body.modal-open {
  overflow: hidden;
}

弹出窗口工作正常。问题是,每当弹出窗口出现时,我仍然可以单击模板的标题菜单和后台的一些链接。如何禁用后台的所有内容,以便仅单击弹出窗口中可用的内容。

【问题讨论】:

  • 使用overlay怎么样?
  • 你需要添加一个不透明度小于0.5的叠加层
  • @SagarV 为什么不透明度必须小于0.5?此属性的任何值都应适用于 OP。
  • @SagarV 这是非常主观的。
  • @SagarV 从设计的角度来看,我同意模态框背后应该有一个透明的覆盖层,以便在视觉上向用户确认其他元素不可点击——这是很好的用户体验。但是,您的评论听起来好像这是一项技术要求,当然不是。

标签: javascript html css modal-dialog


【解决方案1】:

简介

一个简单的解决方案是添加一个覆盖背景的&lt;div&gt;,并位于弹出窗口下方但高于所有其他内容。

下面是一个非常简单的例子,我想你正在尝试做的事情。希望您可以对其进行调整以适应您的情况。

示例

function openModal() {
  $("#overlay").css({"display":"block"});
  $("#modal").css({"display":"block"});
}
#modal {
  position:fixed;
  left:50%;
  top:50%;
  transform:translate(-50%, -50%);
  border:solid 1px #000;
  display:none;
  background-color:#fff;
}

#overlay {
  position:fixed;
  left:0;
  top:0;
  width:100vw;
  height:100vh;
  display:none;
  background-color:#000;
  opacity:0.5;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Awesome Content!

<button onclick="openModal()">Open Modal!</button>

<div id="overlay"></div>
<div id="modal"><h1>Modal Content!</h1></div>

要点

  • #overlay div 位于 #modal div 之前 - 这就是我将模态设置在顶部的方式。或者,您可以在 CSS 中使用 z-index

  • opacity 值在这里不是必需的,它只是用来演示叠加层相对于页面/模态的位置;

  • 这里的实现细节都不重要。 JavaScript 不重要,我的大部分 CSS 也不重要。这个例子只是为了给你指明正确的方向。

【讨论】:

  • 1) 你的模态不会关闭 2) 你不需要设置 body 和 html 的高度,因为你可以使用 width: 100vw; (vw 是视口宽度。100vw 是 100%视口宽度)和height: 100vh;(vh 是视口高度)
  • @JamesDouglas 我的模式不需要关闭——我只是在演示背景覆盖如何工作,而不是 Javascript 如何工作(这很简单)。感谢您提醒我注意 vhvw - 我将编辑我的帖子以改用这些单位。
  • 虽然这不是解决方案,但它有助于对此事有所了解......非常感谢:) (y)
【解决方案2】:

您可以使用覆盖 - 另一个覆盖 html 的屏幕的完整大小的 div,并在主体上提供半透明的灰色阴影。

在本例中,使用两个 div。

一个是叠加层,另一个(为了方便在叠加层内)是模态的。

<div class="overlay">
  <div class="modal">
    This is the modal. You can put whatever you like in here.
  </div>
</div>

现在叠加层需要样式:

.overlay {
  position: fixed; /* Positioning and size */
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(128,128,128,0.5); /* color */
  display: none; /* making it hidden by default */
}

模态也需要一些:

.modal {
  position: fixed; /* positioning in center of page */
  top: 50vh;
  left: 50vw;
  transform: translate(-50%,-50%);
  height: 400px; /* size */
  width: 600px;
  background-color: white; /* background color */
}

通过放置这个来包含 jQuery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

在代码顶部的 head 标签中。

然后,使用此按钮打开模态:

<button onclick="$('.overlay').show();">Open modal</button>

这个 jQuery 代码捕捉点击覆盖而不是它的孩子。

$('.overlay').on('click', function(e) {
  if (e.target !== this) {
    return;
  }
  $('.overlay').hide();
});

$('.overlay').on('click', function(e) {
  if (e.target !== this) {
    return;
  }
  $('.overlay').hide();
});
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(128,128,128,0.5);
  display: none;
}
.modal {
  position: fixed;
  top: 50vh;
  left: 50vw;
  transform: translate(-50%,-50%);
  height: 400px;
  width: 600px;
  background-color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<a href="https://www.google.co.uk">This is a link, but with the modal open you can't click it!</a>
<br>
<br>
<button onclick="$('.overlay').show();">Open modal</button>

<div class="overlay">
  <div class="modal">
    This is the modal. You can put whatever you like in here.
  </div>
</div>

【讨论】:

  • 代码可以运行,但我无法在我的项目中使用它,但它对我解决问题有很大帮助...非常感谢。
【解决方案3】:

这是一个使用 CSS 属性 pointer-events (https://developer.mozilla.org/en/docs/Web/CSS/pointer-events) 的示例。

示例:https://codepen.io/zvona/pen/YxQzEO

关键思想是在模态打开时为正文设置pointer-events: none;。但是您想要与之交互的特定元素应该具有例如pointer-events: auto;.

在示例中,您可以在隐藏对话框时单击两个按钮,但在显示对话框时只能单击 Toggle Dialog 按钮。

【讨论】:

  • 这个答案比其他任何答案都干净,应该成为公认的答案。
  • 同意这个答案看起来比其他答案更清晰、更容易。与这个答案相比,其他人似乎更像是一个黑客。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
相关资源
最近更新 更多