显示活动链接响应菜单

时间:2017-02-21 19:43:24

标签: jquery css menu label responsive

我在响应式菜单顶部显示活动链接时遇到问题,我希望它在选中时显示在顶部,它在选中时已经应用了活动的css样式但它没有显示它在顶部,我不知道如何让它成为可能的标签? 这是我的代码

CSS

@media screen and (max-width:768px) {
    ul.topnav li a {
        display: inline-block;
        color: #fff;
        text-align: center;
        padding: 20px 20px 20px 20px;
        height: auto;
        }   
    ul.topnav .active {
        color: #ee1921;
        border-bottom: none!important;
        font-size: 20px;
        }
    ul.topnav.responsive {
        position: relative;
        }
    ul.topnav.responsive li.icon {
        position: absolute;
        right: 0;
        top: 0;
        }
    ul.topnav.responsive li {
        float: none;
        display: inline;
        }
    ul.topnav.responsive li a {
        display: block;
        text-align: left;
        font-size: 20px;
        padding: 20px 20px 15px 20px;
        }
ul.topnav.responsive .active {
        border-bottom: 1px solid #fff;
        }
}

HTML

<ul class="topnav" id="myTopnav">
    <li><a class="navItem" href="FAQ-sports.html">Sports</a></li>
    <li><a href="FAQ-casino.html"  class="navItem">Casino</a></li>
    <li><a href="#Slots"  class="navItem">Slots</a></li>
    <li><a href="#Vegas"  class="navItem">Vegas</a></li>
    <li><a href="#Bingo"  class="active">Bingo</a></li>
    <li class="icon"><a href="javascript:void(0);" style="font-size:15px;" onclick="myFunction()">☰</a></li>
</ul><br><br>

jquery脚本是:

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

1 个答案:

答案 0 :(得分:0)

您可以在父级上使用display: flex; flex-direction: column;,而不是将.active类更改为li(以便它是flex-parent的flex-child),然后{ {1}}将.active { order: -1; }放在列表的开头。

li
function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
@media screen and (max-width:768px) {
  ul.topnav li a {
    display: inline-block;
    color: #fff;
    text-align: center;
    padding: 20px 20px 20px 20px;
    height: auto;
  }
  ul.topnav .active a {
    color: #ee1921;
    border-bottom: none!important;
    font-size: 20px;
  }
  ul.topnav.responsive {
    position: relative;
  }
  ul.topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  ul.topnav.responsive li {
    float: none;
    display: inline;
  }
  ul.topnav.responsive li a {
    display: block;
    text-align: left;
    font-size: 20px;
    padding: 20px 20px 15px 20px;
  }
  ul.topnav.responsive .active a {
    border-bottom: 1px solid #fff;
  }
  .topnav {
    display: flex;
    flex-direction: column;
  }
  .active {
    order: -1;
  }
}