【问题标题】:Don't preview first item in menu topnav不要在菜单 topnav 中预览第一项
【发布时间】:2018-04-10 10:24:00
【问题描述】:

在这个example(复制下面的代码)中,第一项“主页”始终显示。当窗口足够小时,如何像所有其他项一样隐藏“主页”项?

非常感谢所有帮助,谢谢。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.active {
  background-color: #4CAF50;
  color: white;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child) {display: none;}
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}
</style>
</head>
<body>

<div class="topnav" id="myTopnav">
  <a href="#home" class="active">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
</div>

<div style="padding-left:16px">
  <h2>Responsive Topnav Example</h2>
  <p>Resize the browser window to see how it works.</p>
</div>

<script>
function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
</script>

</body>
</html>

【问题讨论】:

  • 请说明“足够小”的含义:-)
  • 你有 a:not(:first-child) 它会跳过第一个回家的孩子:D
  • @LucianoGiordano “足够小”表示窗口足够小以在顶部导航中显示菜单。

标签: javascript html css


【解决方案1】:

原因是媒体查询指定了 a:not(:first-child) 但其他子项显示:none。在其他中隐藏所有子元素,包括 firstchild 更改

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child) {display: none;}
  .topnav a.icon {
    float: right;
    display: block;
  }
}

to 

@media screen and (max-width: 600px) {
          .topnav a {display: none;}
          .topnav a.icon {
            float: right;
            display: block;
          }
        }

【讨论】:

  • 你应该说出原因
  • 原因如果因为媒体查询指定了a:not(:first-child) 但其他孩子dispay:none;
  • 我知道为什么要为其他人添加您的答案
猜你喜欢
  • 1970-01-01
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多