草庐IT

javascript - Div 容器隐藏在另一个 div 后面,即使 z-index 为 999

coder 2023-08-08 原文

我正在设计一个新的网站布局,但遇到了问题。

我点击一个菜单,触发一些 Javascript 来显示一个菜单容器。这出现使用 jquery 切换功能。它出现一秒钟,然后当它完成时,div 隐藏在我的主要内容 div 后面,所以你看不到它。

我已将子菜单容器上的 z-index 更改为 999,但没有任何区别。

如果您单击主页按钮(在下面的代码中或在 JSFiddle 链接中),您应该明白我的意思。

JSFiddle

html,
body {
  font-family: arial;
  margin: 0;
  padding: 0;
}
h1 {
  font-size: x-large;
  font-weight: bold;
  color: #403537;
}
header {
  margin-left: auto;
  margin-right: auto;
  text-align: center;
  font-size: xx-large;
  font-weight: bold;
  vertical-align: central;
  background-color: #403537;
}
#container {
  width: 1024px;
  margin-left: auto;
  margin-right: auto;
  background-color: #E5E2E4;
  border: #BFBDBE solid thin;
  border-radius: 5px;
  margin-top: 10px;
  margin-bottom: 10px;
  padding: 10px;
}
form.loginForm {
  width: 500px;
  margin-left: auto;
  margin-right: auto;
  background-color: #E5E2E4;
  border: #BFBDBE solid thin;
  border-radius: 5px;
  margin-top: 40px;
  margin-bottom: 10px;
  padding: 10px;
  clear: both;
  white-space: normal;
}
input[type=text],
input[type=password] {
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  transition: border linear .2s, box-shadow linear .2s;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  width: 360px;
  margin-top: 10px;
  margin-bottom: 10px;
  outline: none;
}
input[type=text]:required:focus,
input[type=password]:required:focus {
  box-shadow: 4px 4px 5px rgba(200, 0, 0, 0.40);
  outline: none;
}
input[type=text]:valid:focus,
input[type=password]:valid:focus {
  box-shadow: 4px 4px 5px rgba(27, 127, 18, 0.40);
  outline: none;
}
input[type=submit] {
  width: 120px;
  padding: 10px;
  color: white;
  background: #3239ff;
  /* Old browsers */
  background: -moz-linear-gradient(top, #3239ff 1%, #0d1cc1 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(1%, #3239ff), color-stop(100%, #0d1cc1));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #3239ff 1%, #0d1cc1 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #3239ff 1%, #0d1cc1 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #3239ff 1%, #0d1cc1 100%);
  /* IE10+ */
  background: linear-gradient(to bottom, #3239ff 1%, #0d1cc1 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#3239ff', endColorstr='#0d1cc1', GradientType=0);
  border-radius: 5px;
  border: black solid thin;
  font-size: medium;
  text-shadow: 2px 2px 1px rgba(0, 0, 0, 1);
}
label {
  width: 100px;
  margin-right: 10px;
  text-align: right;
}
#pageTitle {
  width: 800px;
  border-bottom: #BFBDBE solid thin;
  margin-left: auto;
  margin-right: auto;
}
footer {
  color: #BFBDBE;
  text-align: center;
  font-size: small;
  margin-top: 50px;
}
footer a {
  color: #BFBDBE;
  text-decoration: none;
}
footer a:hover {
  text-decoration: underline;
}
#errorToolbox {
  position: absolute;
  width: auto;
  height: auto;
  border-radius: 5px;
  display: none;
  padding: 8px;
  background-color: #403537;
  z-index: 9999;
  color: white;
  box-shadow: 4px 4px 5px rgba(0, 0, 0, 0.40);
}
nav {
  background: #403537;
  /* Old browsers */
  background: -moz-linear-gradient(top, #403537 0%, #665459 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #403537), color-stop(100%, #665459));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #403537 0%, #665459 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #403537 0%, #665459 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #403537 0%, #665459 100%);
  /* IE10+ */
  background: linear-gradient(to bottom, #403537 0%, #665459 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#403537', endColorstr='#665459', GradientType=0);
  height: 40px;
}
.menu {
  width: 1024px;
  margin-left: auto;
  margin-right: auto;
  height: calc(100% - 20px);
  padding: 10px;
}
a.menuHeaderButton {
  width: 100px;
  margin-left: 1px;
  margin-right: 1px;
  height: 100%;
  float: left;
  font-weight: bold;
  color: white;
  text-shadow: 4px 4px 7px rgba(117, 110, 110, 1);
  text-decoration: none;
}
.subMenuContainer {
  background-color: #403537;
  width: 150px;
  padding-top: 10px;
  padding-bottom: 10px;
  clear: both;
  display: none;
  z-index: 999;
}
.menuItem {
  clear: both;
  display: block;
  margin-left: 5px;
  margin-right: 5px;
  padding-top: 8px;
  padding-bottom: 8px;
  border-bottom: #BFBDBE solid thin;
  padding-left: 4px;
  transition: all linear .2s, box-shadow linear .2s;
  z-index: 999;
}
.menuItem:hover {
  background-color: #BFBDBE;
}
a.menuItem {
  text-decoration: none;
  color: white;
  z-index: 999;
}
<!DOCTYPE html>
<html>

<body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script>
    function clickHeader(headerName) {
      //Get the position of the header menu
      var position = $("#header" + headerName).position();
      //alert("Left: " + position.left);
      $(".subMenuContainer").hide();
      $("#" + headerName).css({
        "margin-left": position.left
      })
      $("#" + headerName).toggle("medium");
    }
  </script>
  <header>
    <div style="width: 1024px; margin-left: auto; margin-right: auto;"></div>
  </header>
  <nav>
    <div class="menu"> <a class="menuHeaderButton" id="headerHome" href="javascript:clickHeader('Home');">Home</a>
      <a class="menuHeaderButton" id="headerBlog" href="javascript:clickHeader('Blog');">Blog</a>
      <a class="menuHeaderButton" id="headerCritiMon" href="#">CritiMon</a>

    </div>
    <div class="subMenuContainer" id="Home"> <a class="menuItem" href="#">Item 1</a>
      <a class="menuItem" href="#">Item 2</a>

    </div>
    <div class="subMenuContainer" id="Blog"> <a class="menuItem" href="#">Item 3</a>
      <a class="menuItem" href="#">Item 4</a>

    </div>
  </nav>
  <div id="container">
    <p>Hello, the quick brown fox jumped over the lazy dogs back.</p>
    <p>All good men must come to the aid of the party.</p>
  </div>
  <footer>&copy; 2010 - 2015 | <a href="#">Privacy Policy</a> 
  </footer>
</body>

</html>

最佳答案

您在 .subMenuContainer 上忘记了 position: absolute;z-index 不影响静态定位的元素。

For a positioned box, the 'z-index' property specifies:

  1. The stack level of the box in the current stacking context.
  2. Whether the box establishes a stacking context.

http://www.w3.org/TR/CSS2/visuren.html#propdef-z-index

(强调我的)

.subMenuContainer {
  background-color: #403537;
  <b><i>position: absolute;</i></b>
  width: 150px;
  padding-top: 10px;
  padding-bottom: 10px;
  clear: both;
  display: none;
  z-index: 999;
}

html,
body {
  font-family: arial;
  margin: 0;
  padding: 0;
}
h1 {
  font-size: x-large;
  font-weight: bold;
  color: #403537;
}
header {
  margin-left: auto;
  margin-right: auto;
  text-align: center;
  font-size: xx-large;
  font-weight: bold;
  vertical-align: central;
  background-color: #403537;
}
#container {
  width: 1024px;
  margin-left: auto;
  margin-right: auto;
  background-color: #E5E2E4;
  border: #BFBDBE solid thin;
  border-radius: 5px;
  margin-top: 10px;
  margin-bottom: 10px;
  padding: 10px;
}
form.loginForm {
  width: 500px;
  margin-left: auto;
  margin-right: auto;
  background-color: #E5E2E4;
  border: #BFBDBE solid thin;
  border-radius: 5px;
  margin-top: 40px;
  margin-bottom: 10px;
  padding: 10px;
  clear: both;
  white-space: normal;
}
input[type=text],
input[type=password] {
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  transition: border linear .2s, box-shadow linear .2s;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
  width: 360px;
  margin-top: 10px;
  margin-bottom: 10px;
  outline: none;
}
input[type=text]:required:focus,
input[type=password]:required:focus {
  box-shadow: 4px 4px 5px rgba(200, 0, 0, 0.40);
  outline: none;
}
input[type=text]:valid:focus,
input[type=password]:valid:focus {
  box-shadow: 4px 4px 5px rgba(27, 127, 18, 0.40);
  outline: none;
}
input[type=submit] {
  width: 120px;
  padding: 10px;
  color: white;
  background: #3239ff;
  /* Old browsers */
  background: -moz-linear-gradient(top, #3239ff 1%, #0d1cc1 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(1%, #3239ff), color-stop(100%, #0d1cc1));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #3239ff 1%, #0d1cc1 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #3239ff 1%, #0d1cc1 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #3239ff 1%, #0d1cc1 100%);
  /* IE10+ */
  background: linear-gradient(to bottom, #3239ff 1%, #0d1cc1 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#3239ff', endColorstr='#0d1cc1', GradientType=0);
  border-radius: 5px;
  border: black solid thin;
  font-size: medium;
  text-shadow: 2px 2px 1px rgba(0, 0, 0, 1);
}
label {
  width: 100px;
  margin-right: 10px;
  text-align: right;
}
#pageTitle {
  width: 800px;
  border-bottom: #BFBDBE solid thin;
  margin-left: auto;
  margin-right: auto;
}
footer {
  color: #BFBDBE;
  text-align: center;
  font-size: small;
  margin-top: 50px;
}
footer a {
  color: #BFBDBE;
  text-decoration: none;
}
footer a:hover {
  text-decoration: underline;
}
#errorToolbox {
  position: absolute;
  width: auto;
  height: auto;
  border-radius: 5px;
  display: none;
  padding: 8px;
  background-color: #403537;
  z-index: 9999;
  color: white;
  box-shadow: 4px 4px 5px rgba(0, 0, 0, 0.40);
}
nav {
  background: #403537;
  /* Old browsers */
  background: -moz-linear-gradient(top, #403537 0%, #665459 100%);
  /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #403537), color-stop(100%, #665459));
  /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, #403537 0%, #665459 100%);
  /* Chrome10+,Safari5.1+ */
  background: -o-linear-gradient(top, #403537 0%, #665459 100%);
  /* Opera 11.10+ */
  background: -ms-linear-gradient(top, #403537 0%, #665459 100%);
  /* IE10+ */
  background: linear-gradient(to bottom, #403537 0%, #665459 100%);
  /* W3C */
  filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#403537', endColorstr='#665459', GradientType=0);
  height: 40px;
}
.menu {
  width: 1024px;
  margin-left: auto;
  margin-right: auto;
  height: calc(100% - 20px);
  padding: 10px;
}
a.menuHeaderButton {
  width: 100px;
  margin-left: 1px;
  margin-right: 1px;
  height: 100%;
  float: left;
  font-weight: bold;
  color: white;
  text-shadow: 4px 4px 7px rgba(117, 110, 110, 1);
  text-decoration: none;
}
.subMenuContainer {
  background-color: #403537;
  position: absolute;
  width: 150px;
  padding-top: 10px;
  padding-bottom: 10px;
  clear: both;
  display: none;
  z-index: 999;
}
.menuItem {
  clear: both;
  display: block;
  margin-left: 5px;
  margin-right: 5px;
  padding-top: 8px;
  padding-bottom: 8px;
  border-bottom: #BFBDBE solid thin;
  padding-left: 4px;
  transition: all linear .2s, box-shadow linear .2s;
  z-index: 999;
}
.menuItem:hover {
  background-color: #BFBDBE;
}
a.menuItem {
  text-decoration: none;
  color: white;
  z-index: 999;
}
<!DOCTYPE html>
<html>

<body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script>
    function clickHeader(headerName) {
      //Get the position of the header menu
      var position = $("#header" + headerName).position();
      //alert("Left: " + position.left);
      $(".subMenuContainer").hide();
      $("#" + headerName).css({
        "margin-left": position.left
      })
      $("#" + headerName).toggle("medium");
    }
  </script>
  <header>
    <div style="width: 1024px; margin-left: auto; margin-right: auto;"></div>
  </header>
  <nav>
    <div class="menu"> <a class="menuHeaderButton" id="headerHome" href="javascript:clickHeader('Home');">Home</a>
      <a class="menuHeaderButton" id="headerBlog" href="javascript:clickHeader('Blog');">Blog</a>
      <a class="menuHeaderButton" id="headerCritiMon" href="#">CritiMon</a>

    </div>
    <div class="subMenuContainer" id="Home"> <a class="menuItem" href="#">Item 1</a>
      <a class="menuItem" href="#">Item 2</a>

    </div>
    <div class="subMenuContainer" id="Blog"> <a class="menuItem" href="#">Item 3</a>
      <a class="menuItem" href="#">Item 4</a>

    </div>
  </nav>
  <div id="container">
    <p>Hello, the quick brown fox jumped over the lazy dogs back.</p>
    <p>All good men must come to the aid of the party.</p>
  </div>
  <footer>&copy; 2010 - 2015 | <a href="#">Privacy Policy</a> 
  </footer>
</body>

</html>

关于javascript - Div 容器隐藏在另一个 div 后面,即使 z-index 为 999,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32020617/

有关javascript - Div 容器隐藏在另一个 div 后面,即使 z-index 为 999的更多相关文章

  1. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  2. ruby - 即使失败也继续进行多主机测试 - 2

    我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r

  3. ruby-on-rails - Rails - 从另一个模型中创建一个模型的实例 - 2

    我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案

  4. ruby-on-rails - Nokogiri:使用 XPath 搜索 <div> - 2

    我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll

  5. ruby - 一个 YAML 对象可以引用另一个吗? - 2

    我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的ruby​​yaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir

  6. ruby - 将一个超薄文件包含在另一个超薄文件中 - 2

    我在一个静态网站上工作(因此没有真正的服务器支持),我想在另一个网站中包含一个小的细长片段,可能会向它传递一个变量。这可能吗?在rails中很容易,虽然是render方法,但我不知道如何在slim上做(显然load方法不适用于slim)。 最佳答案 Slim包含Include插件,允许在编译时直接在模板文件中包含其他文件:require'slim/include'includepartial_name文档可在此处获得:https://github.com/slim-template/slim/blob/master/doc/incl

  7. ruby - 如何使用 Selenium Webdriver 根据 div 的内容执行操作? - 2

    我有一个使用SeleniumWebdriver和Nokogiri的Ruby应用程序。我想选择一个类,然后对于那个类对应的每个div,我想根据div的内容执行一个Action。例如,我正在解析以下页面:https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=puppies这是一个搜索结果页面,我正在寻找描述中包含“Adoption”一词的第一个结果。因此机器人应该寻找带有className:"result"的div,对于每个检查它的.descriptiondiv是否包含单词“adoption

  8. ruby-on-rails - 如何使用 ruby​​ 从 self 方法调用另一个方法? - 2

    #app/models/product.rbclassProduct我从Controller调用方法1。当我运行程序时。我收到一个错误:method_missing(atlinemethod2(param2)).rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-5.0.0/lib/active_record/relation/batches.rb:59:in`block(2levels)infind_each... 最佳答案 classProduct说明:第一个是类

  9. ruby-on-rails - Sphinx - 何时对字段使用 'has' 和 'indexes' - 2

    我几天前在我的ruby​​onrails2.3.2上安装了Sphinx和Thinking-Sphinx,基本搜索效果很好。这意味着,没有任何条件。现在,我想用一些条件过滤搜索。我有公告模型,索引如下所示:define_indexdoindexestitle,:as=>:title,:sortable=>trueindexesdescription,:as=>:description,:sortable=>trueend也许我错了,但我注意到只有当我将:sortable=>true语法添加到这些属性时,我才能将它们用作搜索条件。否则它找不到任何东西。现在,我还在使用acts_as_tag

  10. 即使安装了 gem,Ruby 也找不到所需的库 - 2

    我花了几天时间尝试安装ruby​​1.9.2并让它与gems一起工作:-/我最终放弃了我的MacOSX10.6机器,下面是我的Ubuntu机器上的当前状态。任何建议将不胜感激!#rubytest.rb:29:in`require':nosuchfiletoload--mongo(LoadError)from:29:in`require'fromtest.rb:1:in`'#cattest.rbrequire'mongo'db=Mongo::Connection.new.db("mydb")#gemwhichmongo/usr/local/rvm/gems/ruby-1.9.2-p0/g

随机推荐