javascript - 如何定义一个div覆盖另一个div内的页面?

标签 javascript jquery html css z-index

我正在制作一个包含一些容器的网站,当单击容器时,应该会显示一个弹出窗口并覆盖网站中的其他所有内容(即其他容器和 float 菜单)。

类似这样的事情: HTML

<div class="container">
  This is the container.
  <div class="inside">
      Some popup content that will be specific to this container, and should cover everything.
  </div>
</div>
<div class="container">
  This is another container.
  <div class="inside">
      Some popup content that will be specific to this container, and should cover everything.
  </div>
</div>
<div class="testing">
  This is a floating menu that will be controlled by javascript later.<br>
  Necessary to be covering the container, but should be cover by the inside div.<br>
  <a href="#">Some fake links</a><br>
  <a href="#">Some fake links</a><br>
  <a href="#">Some fake links</a><br>
  <a href="#">Some fake links</a><br>
  <a href="#">Some fake links</a><br>
</div>

CSS

div.container {
  position: relative;
  height: 300px;
  width: 300px;
  background-color: green;
  color: white;
  z-index: 1;
  margin-bottom: 10px;
}
div.inside {
  position: fixed;
  top: 0px;
  left: 0px;
  height: 100%;
  width: 100%;
  text-align: center;
  padding-top: 30%;
  background-color: #000;
  opacity: .8;
  z-index: 100;
}
div.testing {
  position: absolute;
  display: block;
  top: 40px;
  left: 40px;
  background-color: white;
  color: red;
  z-index: 10;
}

Javascript(假设已包含 jquery)

$("div.inside").hide();
$("div.container").click(function(){
    $(this).find("div.inside").show();
})
$("div.inside").click(function(event){
    event.stopPropagation();
    $('div.inside').hide();
})

Here's a jsFiddle to demonstrate the current situation

正如您在上面的演示中看到的,弹出窗口并未覆盖其他所有内容。由于弹出内容与容器高度相关,因此我希望尽可能将其保留在容器内。 (并且编写 javascript 更容易,因为不需要给出 id。)

有没有办法保持 HTML 结构相同,但仅使用 CSS 使弹出窗口覆盖其他所有内容?

最佳答案

在非静态定位元素的情况下,嵌套很重要。如果元素 B 位于元素 A 之上,则元素 A 的子元素永远不能高于元素 B。

更多信息请参见:https://css-tricks.com/almanac/properties/z/z-index/

从除弹出窗口之外的所有内容中删除所有 Z 索引

关于javascript - 如何定义一个div覆盖另一个div内的页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34260762/

相关文章:

javascript - 使用文件系统 api 检索所有目录

javascript - 具有Docker卷的Node.js开发环境

javascript - 显示隐藏 html 元素并更新按钮文本

php - 提供生成的文件以供从 jQuery post 下载

javascript - Switchery(单选按钮)值到数据库

javascript - jquery 滚动动画不工作

javascript - 文件未使用 Node js上传到服务器

javascript - jQuery 在最后一个框中显示按钮

javascript - 我如何用动态输入框求和总值?

javascript - 创建包含 3 个部分的圆,并在 HTML/css/script 中对每个部分执行操作