在较大屏幕尺寸下消失导航

问题描述 投票:2回答:1

我有一些导航html,我试图做出响应。我成功地让汉堡包菜单出现在小于1050像素的屏幕尺寸上,并且它可以打开和关闭菜单。问题是,如果在使屏幕变大之前没有关闭汉堡包菜单,则导航菜单会停留在显示屏:无模式且不显示。所以看起来导航突然失踪了。我认为普通的css会占用更大的屏幕大小,但可能是因为该属性是在一个脚本中设置的。任何人都可以帮我解决这个问题。我对响应式设计还不熟悉。

HTML:

<nav>
<div class="navigation">
    <a href="/index.html"> <img class="nav-img" src="/incl/image.png"> </a>

    <button id="mobile" type="button" onclick="toggleFunction()">
          <img src="/incl/menu.png" width="35px" height="35px" alt="menu">
    </button>

    <ul id="nav-list">
        <li> <a href="/proposer/">Proposer</a> </li>
        <li> <a href="/cda/">Archive</a> </li>
        <li> <a href="/ciao/">Data<br/>Analysis</a> </li>
        <li> <a href="/cal/">Instruments and<br/>Calibration</a> </li>
        <li> <a href="/public/">For The<br/>Public</a> </li>
    </ul>
</div></nav>

<script>
    function toggleFunction() {
        var x = document.getElementById('nav-list');

        if (x.style.display === 'block') {
            x.style.display = 'none';
        } else {
            x.style.display = 'block';
        }
    }
</script>

CSS:

#nav-list {
list-style-type: none;
padding-top: 30px;
padding-right: 20px;
margin: 0px;
overflow: hidden;
display: inline-table;
float: right;
}

#nav-list li {
display: table-cell;
vertical-align: middle;
padding: 0px 15px;
margin: 0px;
text-align: center;
line-height: 110%;
font-size: 1.3em;
font-family: Helvetica, Arial, sans-serif;
font-weight: 400;
}

#mobile {
display: none;
}

@media only screen and (max-width: 1050px) {
.nav-img {
    margin: 0px 5px 5px 5px;
}

#mobile {
    display: inline;
    float: right;
    background-color: transparent;
    border: 1px solid #333;
    border-radius: 5px;
    margin: 10px 10px 10px 0px;
    padding: 5px;
}

#nav-list {
    list-style-type: none;
    margin: 0px;
    padding: 0px;
    overflow: hidden;
    display: none;
    float: none
}

#nav-list li {
    display: block;
    text-align: center;
    margin: 10px 0px 10px 0px;
    line-height: 110%;
    font-size: 1em;
}
javascript jquery html css responsive-design
1个回答
0
投票

解决这个问题的最简单方法是toggling a class关于#nav-list元素。

通过此更改,该类仅在移动媒体查询中的CSS中具有含义,其中它将#nav-list更改为display: block。无论是否切换,该类都不会影响大于1050px的屏幕。

function toggleFunction() {
  var x = document.getElementById('nav-list');
  x.classList.toggle('show');
}
#nav-list {
  list-style-type: none;
  padding-top: 30px;
  padding-right: 20px;
  margin: 0px;
  overflow: hidden;
  display: inline-table;
  float: right;
}

#nav-list li {
  display: table-cell;
  vertical-align: middle;
  padding: 0px 15px;
  margin: 0px;
  text-align: center;
  line-height: 110%;
  font-size: 1.3em;
  font-family: Helvetica, Arial, sans-serif;
  font-weight: 400;
}

#mobile {
  display: none;
}

@media only screen and (max-width: 1050px) {
  .nav-img {
    margin: 0px 5px 5px 5px;
  }

  #mobile {
    display: inline;
    float: right;
    background-color: transparent;
    border: 1px solid #333;
    border-radius: 5px;
    margin: 10px 10px 10px 0px;
    padding: 5px;
  }

  #nav-list {
    list-style-type: none;
    margin: 0px;
    padding: 0px;
    overflow: hidden;
    display: none;
    float: none
  }

  /* show navigation based on class */
  #nav-list.show {
    display: block;
  }

  #nav-list li {
    display: block;
    text-align: center;
    margin: 10px 0px 10px 0px;
    line-height: 110%;
    font-size: 1em;
  }
}
<nav>
  <div class="navigation">
    <a href="/index.html"> <img class="nav-img" src="/incl/image.png"> </a>

    <button id="mobile" type="button" onclick="toggleFunction()">
          <img src="/incl/menu.png" width="35px" height="35px" alt="menu">
    </button>

    <ul id="nav-list">
      <li> <a href="/proposer/">Proposer</a> </li>
      <li> <a href="/cda/">Archive</a> </li>
      <li> <a href="/ciao/">Data<br/>Analysis</a> </li>
      <li> <a href="/cal/">Instruments and<br/>Calibration</a> </li>
      <li> <a href="/public/">For The<br/>Public</a> </li>
    </ul>
  </div>
</nav>
© www.soinside.com 2019 - 2024. All rights reserved.