【问题标题】:Disappearing navigation at larger screen size在更大的屏幕尺寸下消失的导航
【发布时间】:2018-06-02 05:12:42
【问题描述】:

我有一些导航 html,我正在尝试使其具有响应性。我已经成功地让汉堡菜单出现在小于 1050 像素的屏幕尺寸上,并且它可以打开和关闭菜单。问题是,如果在放大屏幕之前不关闭汉堡菜单,导航菜单就会卡在 display: none 模式并且不显示。所以看起来导航突然不见了。我认为普通的 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】:

    解决此问题的最简单方法是在#nav-list 元素上使用toggling a class

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

    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>

    【讨论】:

    • 成功了!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    相关资源
    最近更新 更多