css 模态未覆盖整个分区

pinkon5k  于 2022-12-24  发布在  其他
关注(0)|答案(2)|浏览(115)

所以,我一直在尝试做一个模态,onclick涵盖了页面上的一切,除了模态,但它不工作。
套印格式只覆盖页面的底部而不覆盖顶部。
截图:https://snipboard.io/g20yWq.jpg
代码片段:

var taskModal = document.getElementById("taskContainer")
var addBtn = document.getElementById("addButton")
var close = document.getElementsByClassName("close")

addBtn.onclick = function() {
    taskModal.style.display = "block";
}
close.onclick = function() {
    taskModal.style.display = "none";
}
window.onclick = function(event) {
    if (event.target == taskModal) {
      taskModal.style.display = "none";
    }
  }
.taskContainer {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
  height: 100%;
  width: 100%;
  position: relative;
  top: 5pc;
}
#taskContainerContent {
  background-color: #333;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 50%;
  height: 50%;
}
.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
}
<div class="taskContainer" id="taskContainer">
      <div id="taskContainerContent">
      <span class="close">&times;</span>
      </div>
    </div>

idk为什么代码片段不工作,但好。有人知道为什么它不能正确覆盖吗?

rkkpypqq

rkkpypqq1#

试试这个款式。

.taskContainer {
  display: none; /* Hidden by default . change it with javascript to:  flex */
  justify-content: center;
  align-items: center;   /* first 3 items are for centered #taskContainerContent */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
  height: 100vh; 
  width: 100vw;
  position: fixed; /* Stay in place */
  top: 0;
  left: 0;
  z-index: 100; /* another z indexes must be lower 100 */
}
#taskContainerContent {
  background-color: #333;
  padding: 20px;
  border: 1px solid #888;
  width: 50%;
  height: 50%;
}
.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
}
brc7rcf0

brc7rcf02#

尝试将位置更改为绝对位置,并将top、left、bottom和right值全部设置为0。此外,单击时的关闭按钮需要包含索引才能正常工作
参见以下片段

var taskModal = document.getElementById("taskContainer")
var addBtn = document.getElementById("addButton")
var close = document.getElementsByClassName("close")

addBtn.onclick = function() {
  taskModal.style.display = "block";
}

close[0].onclick = function () {
  taskModal.style.display = "none";
};

window.onclick = function(event) {
  if (event.target == taskModal) {
    taskModal.style.display = "none";
  }
}
.taskContainer {
  display: none;  /* Hidden by default */
  overflow: auto;  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);  /* Black w/ opacity */
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

#taskContainerContent {
  background-color: #333;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 50%;
  height: 50%;
}

.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
}
<button type="button" id="addButton">Show Modal</button>
<div class="taskContainer" id="taskContainer">
  <div id="taskContainerContent">
    <span class="close">&times;</span>
  </div>
</div>

相关问题