javascript - 模式打开时锁定背景

标签 javascript jquery html css modal-dialog

我想知道如何在模式打开时锁定背景。我在这里看到的所有答案都与引导模式有关。这是我从 W3 抓到的。

模式中将包含更多内容。话虽如此,模式将垂直滚动,背景内容将保持静态(当模式打开时)。

https://jsfiddle.net/p8aw4fer/

    // Get the modal

var modal = document.getElementById('detailmodal');


// Get the button that opens the modal
var btn = document.getElementsByClassName("modalbutton");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close");

// When the user clicks the buttons open the modal 
for (let i = 0; i < btn.length; i++) {
  btn[i].onclick = function() {
  document.getElementsByClassName('modal')[i].style.display = "block";
  }
}

	for (let i = 0; i < span.length; i++) {
// When the user clicks on <span> (x), close the modal
span[i].onclick = function() {
  document.getElementsByClassName('modal')[i].style.display = "none";
}
}
// When the user clicks anywhere outside of the modal, close it
for (let i = 0; i < window.length; i++) {
window[i].onclick = function(event) {
  if (event.target == modal) {
    document.getElementsByClassName('modal')[i].style.display = 
 "none";
  }
}}
      /* The Modal (background) */
  .modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: hidden; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ 
  }

  .textinside{
    font-size: 40px;
  }


  /* Modal Content */
  .modal-content {
      padding: 1.25rem;
      position: relative;
      overflow: auto;
      float: right;
      background-color: #fff;
      margin: auto;
      border: 1px solid #888;
      max-width: 250px;
      width: 100%;
      height: 100%;
      box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
      -webkit-animation-name: animatetop;
      -webkit-animation-duration: 0.4s;
      animation-name: animatetop;
      animation-duration: 0.8s;
  }


  /* Add Animation */
  @-webkit-keyframes animatetop {
    from {right:-100px; opacity:0} 
    to {right:0; opacity:1}
  }

  @keyframes animatetop {
    from {right:-100px; opacity:0}
    to {right:0; opacity:1}
  }


  /* The Close Button */
  .close {
    color: #7F7F7F;
    font-size: 3rem;
      float: right;
      top: 0;
  }

  .close:hover,
  .close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;

  }
        <button class="modalbutton"><a href="#">CLICK ME :)</a></button><br>
    <div class="textinside">
    Here as place holder to see the locked background<br>
    Here as place holder to see the locked background<br>
    Here as place holder to see the locked background<br>
    Here as place holder to see the locked background<br>
    Here as place holder to see the locked background<br>
    Here as place holder to see the locked background<br>
    Here as place holder to see the locked background<br>
    Here as place holder to see the locked background<br>
    Here as place holder to see the locked background<br>
    Here as place holder to see the locked background<br>
    </div>

    <div class="modal">

    <div class="modal-content">
      <div class="modal-header">
        <span class="close">&times;</span>
        <h2>2 Modal Header</h2>
      </div>
      <div class="modal-body">
        <p>please link with 2</p>
        <p>Some other text...</p>
      </div>
      <div class="modal-footer">
        <h3>Modal Footer</h3>
      </div>
    </div>

    </div>

最佳答案

这里的技巧是当模态可用时将主体的溢出设置为隐藏,然后在模态隐藏时重置规则。向代码片段添加了更多文本以更有效地显示这一点。

// Get the modal

var modal = document.getElementById('detailmodal');


// Get the button that opens the modal
var btn = document.getElementsByClassName("modalbutton");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close");

// When the user clicks the buttons open the modal 
for (let i = 0; i < btn.length; i++) {
  btn[i].onclick = function() {
    document.getElementsByClassName('modal')[i].style.display = "block";
    document.body.classList.add('overflowHidden');
  }
}

for (let i = 0; i < span.length; i++) {
  // When the user clicks on <span> (x), close the modal
  span[i].onclick = function() {
    document.getElementsByClassName('modal')[i].style.display = "none";
    document.body.classList.remove('overflowHidden');
  }
}
// When the user clicks anywhere outside of the modal, close it
for (let i = 0; i < window.length; i++) {
  window[i].onclick = function(event) {
    if (event.target == modal) {
      document.getElementsByClassName('modal')[i].style.display =
        "none";
      document.body.classList.remove('overflowHidden');
    }
  }
}
/* The Modal (background) */

.overflowHidden {
  overflow: hidden !important;
}

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: hidden;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}

.textinside {
  font-size: 40px;
}


/* Modal Content */

.modal-content {
  padding: 1.25rem;
  position: relative;
  overflow: auto;
  float: right;
  background-color: #fff;
  margin: auto;
  border: 1px solid #888;
  max-width: 250px;
  width: 100%;
  height: 100%;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  -webkit-animation-name: animatetop;
  -webkit-animation-duration: 0.4s;
  animation-name: animatetop;
  animation-duration: 0.8s;
}


/* Add Animation */

@-webkit-keyframes animatetop {
  from {
    right: -100px;
    opacity: 0
  }
  to {
    right: 0;
    opacity: 1
  }
}

@keyframes animatetop {
  from {
    right: -100px;
    opacity: 0
  }
  to {
    right: 0;
    opacity: 1
  }
}


/* The Close Button */

.close {
  color: #7F7F7F;
  font-size: 3rem;
  float: right;
  top: 0;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
<button class="modalbutton"><a href="#">CLICK ME :)</a></button><br>
<div class="textinside">
  Here as place holder to see the locked background<br> Here as place holder to see the locked background<br> Here as place holder to see the locked background<br> Here as place holder to see the locked background<br> Here as place holder to see the locked
  background
  <br> Here as place holder to see the locked background<br> Here as place holder to see the locked background<br> Here as place holder to see the locked background<br> Here as place holder to see the locked background<br> Here as place holder to see
  the locked background<br> Here as place holder to see the locked background<br> Here as place holder to see the locked background<br> Here as place holder to see the locked background<br> Here as place holder to see the locked background<br> Here as
  place holder to see the locked background<br> Here as place holder to see the locked background<br> Here as place holder to see the locked background<br> Here as place holder to see the locked background<br> Here as place holder to see the locked background<br>
</div>

<div class="modal">

  <div class="modal-content">
    <div class="modal-header">
      <span class="close">&times;</span>
      <h2>2 Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>please link with 2</p>
      <p>Some other text...</p>
      <p>Some other text...</p>
      <p>Some other text...</p>
      <p>Some other text...</p>
      <p>Some other text...</p>
      <p>Some other text...</p>
      <p>Some other text...</p>
      <p>Some other text...</p>
      <p>Some other text...</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>

关于javascript - 模式打开时锁定背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54188481/

相关文章:

javascript - 在 jQuery 实时预览 div 中将文本转换为图像

javascript - 如何将这个参数传递给这个函数?

javascript - 如何使用Electron的Shell API打开保存在用户目录中的文件

javascript - Image SRC 没有改变什么?

php - 图片上传到文件夹中。但在浏览器中无法查看

javascript - 设置带有 knockout 的 select2 ajax 的初始值

javascript - Ember.js:如何跟踪延迟操作运行时错误?

javascript - 单页应用程序 - 我应该在服务器端使用 MVC 框架吗?

javascript - jQuery:如果这个 HREF 包含

javascript - 通过 Javascript 创建的元素上的 jQuery